EE5805-java-Lecture1 Introduction to java

暑期选择了java减轻下学期的课量,在本科期间就已经学过Java了,现在写一些笔记作为复习。

The Goal

• To understand the Java runtime environment.
• To know Java’s terminologies, advantages and characteristics.
• To recognize the form of identifiers.
• To learn Java primitive data types and object types.
• To become familiar with Java programming style and naming conventions.
• To create, compile, and run a simple Java program.
• To understand method overloading.
• To determine the scope of local variables.
• To understand Array of Java.
• To recognize the multidimensional form of arrays.
• To learn common String operations in Java.
• To use the Character class to process a single character.
• To use the StringBuffer class to process flexible strings.
• To learn how to pass strings to the main method from the command line.

Why Java

  • Simple and Object-oriented
  • Distributed
  • Interpreted,portable and architecture-neutral
  • Robust and secure
  • Dynamic

Java is OO Language

OO encourages the use of object to model or represent program
At here,class(definition of objects),is a blueprint for building objects or a construct which you can specify the features.
Data field and method defined in a class.
请添加图片描述

Modifier and method

  • A modifier is used to specify the properties of the data,
    methods, and classes.
  • Some common modifiers are public, protected, private, final,
    static and abstract.

Identifiers

letters, digits,_ and $
1:cannot be start with digits
2:cannot be reserved word
3:cannot be true/false/or null

The process

1:Declaration(int x;)
2:Assignment(x=1;)
3:combine 1 and 2 ->Initialization(int x=1;)

Constants

We can use final to decorate the variable
  对于一个final变量,如果是基本数据类型的变量,则其数值一旦在初始化之后便不能更改;如果是引用类型的变量,则在对其初始化之后便不能再让其指向另一个对象。

Comments

  1. // single line
  2. /* multiple
    lines
    */
  3. /** multiple
    line
    */

Naming Conventions

  1. variable and method names:aaaBbb
  2. class names:AaaBbb
  3. constants: AB(all Upper letter)

Passing Arguments(传参)

here parameters:形式参数
arguments:实际参数
There are two ways to pass an grgument
1:passed by value(all primitive data belongs this type)
2:passed by reference(objects are passed by reference)

Overloading methods

Here,you should know firstly:
Method signature=method name+parameter lists

So,the Overloading method means:Two methods can be declared with different signatures, hence using same name but different parameter lists is allowed(test)

Scope of Variables

  1. Data field(of class):inside a class and outside a method
  2. local variable(of method):inside a method

Arrays

  1. This is a data structure
  2. Java Array is an object which is a reference type, no matter what type of elements it contains
  3. An array’s size is fixed once created
    -> arrayRefVar.length
  4. Default value on creation
    -> 0 for the numeric primitive data types,
    -> ‘\u0000’ for char types, and
    -> false for boolean types.
  5. Array’s elements accessed through index
    -> array indices are 0-based
  6. Cannot same with int creation and assignment
    请添加图片描述
    should:
    请添加图片描述
    For the two-dimensional arrays:
    请添加图片描述
    array.length:->row
    array[0].length:->col

The string class

请添加图片描述

  1. construct a string:
  • String message =“welcome to Java”
  • String message=new String(“welcom……”)
  • String s=new String();
    (String object is immutable )
  1. Finding length:
  • message.length()(returns 7)
  1. Concatenating Strings:
  • concat()/or is same
    请添加图片描述
  1. Extracting Substrings
  • substring(start, end)returns a string from start to (end-1)
  1. Comparing Strings
  • Using equals()/**==**operator to compare two strings
  • 请添加图片描述
  1. Converting Strings
  • The contents of a string cannot be changed once the string is created.But you can convert a string to a new string using the following methods
  • 请添加图片描述
  1. Searching a character or a substring
  • To search forward, use indexOf();
  • To search backward, use lastIndexOf();
  • 请添加图片描述
  1. Converting Other Types to Strings
  • use valueOf()

The character class

  1. To construct a Character object: Character charObject=new Character(‘b’);
  2. To compare two characters:
    请添加图片描述

The StringBuffer Class(Thread Safe)

  1. The StringBuffer class is an alternative to the String class. In general, a StringBuffer can be used wherever a string is used (drop-in replacement)
  2. StringBuffer is mutable, hence more flexible than String
public class StringBufferDemo {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Hello");
        sb.append(" World");
        sb.insert(5, ",");
        sb.delete(5, 6);
        System.out.println(sb.toString());
    }
}
InputHello, World

Appending Contents to StringBuffer

请添加图片描述

Passing Parameters to Main Method

  1. Method1:请添加图片描述
    类A的main方法:
    首先,定义了一个字符串数组strings,其中包含三个城市名称:“New York”、“Boston"和"Atlanta”。
    然后,调用类B的main方法,并将strings数组作为参数传递给它。

类B的main方法:
接受一个字符串数组args作为参数。
使用一个for循环遍历args数组,并将每个元素打印到控制台。
执行流程

当你运行类A的main方法时,执行流程如下:
类A的main方法被调用。
创建一个字符串数组strings,内容为{“New York”, “Boston”, “Atlanta”}。
调用类B的main方法,并将strings数组传递给它。
类B的main方法开始执行,接收传递过来的strings数组作为参数args。
类B的main方法遍历args数组,并逐个打印数组中的元素。
输出结果

运行类A的main方法时,控制台将输出:
New York
Boston
Atlanta

总结
类A的main方法创建并初始化了一个字符串数组,然后将其传递给类B的main方法。
类B的main方法接收这个数组并打印其内容。
这种调用方式展示了如何在Java中从一个类的main方法调用另一个类的main方法,并传递参数

Static and non-static Object context

在 Java 中,staticnon-static 是两个关键字,用于定义类成员变量和方法。它们之间的主要区别如下:

  1. 内存分配:static 成员变量和方法是在类加载时分配内存,而 non-static 成员变量和方法是在实例化对象时分配内存。
  2. 访问方式:static 成员变量和方法可以通过类名直接访问,而 non-static 成员变量和方法只能通过实例化对象访问。
  3. 初始化:static 成员变量和方法只会被初始化一次,而 non-static 成员变量和方法每次实例化对象时都会被初始化。
  4. 作用域:static 成员变量和方法的作用域是整个类,而 non-static 成员变量和方法的作用域是实例化对象。
  5. 用途:static 成员变量和方法通常用于表示类级别的属性和方法,如常量、工具方法等,而 non-static 成员变量和方法通常用于表示对象级别的属性和方法,如实例变量、实例方法等。

下面是一个示例代码,演示了 staticnon-static 成员变量和方法的用法:

public class MyClass {
    // static 成员变量
    static int staticVar = 0;
    // non-static 成员变量
    int nonStaticVar = 0;

    // static 方法
    static void staticMethod() {
        System.out.println("This is a static method.");
    }

    // non-static 方法
    void nonStaticMethod() {
        System.out.println("This is a non-static method.");
    }

    public static void main(String[] args) {
        // 直接访问 static 成员变量和方法
        System.out.println(MyClass.staticVar);
        MyClass.staticMethod();

        // 实例化对象后访问 non-static 成员变量和方法
        MyClass obj = new MyClass();
        System.out.println(obj.nonStaticVar);
        obj.nonStaticMethod();
    }
}

输出结果为:

0
This is a static method.
0
This is a non-static method.

总之,staticnon-static 是 Java 中两个重要的关键字,它们分别用于定义类级别和对象级别的属性和方法。根据具体的需求,我们可以选择使用它们中的一个或多个来实现我们的程序逻辑。

  • 26
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值