【CS 61b study notes 2】Linked List & Array

Basic data info

Declaring a Variable

  • Variable Definition

    • className variableName = new className ();
      • for reference type
      • e.g. : IntList L1 = new IntList(5, null);
    • primitiveType variableName = value;
      • primitive types are built-in data type in java and can be used directly without using any “new” keyword.
      • e.g. : int x = 10;
    • Some rules
      • A variable name can consist of Capital letters A-Z , lowercase letters a-z , digits 0-9 and two special character such as _underscore and $ dollar sign.
      • First character can not be digit
      • Can NOT use Blank spaces and java keywords
      • No limit in the length ,but by convention 4 to 15 chars
      • Always exist on the left-hand side of assignment operators =
      • Some vaild example : variableName , variablename , _variableName ,$variableName , variableName1, variableName_1, VARIABLENAME
  • Example

    String str1 = "abc";
    String str2 = "abc";
    
    String str3 = new String("abc");
    String str4 = new String("abc");
    String str5 = str3;
    
    System.out.println(str1 == str2); // true
    System.out.println(str3 == str4); // false
    System.out.println(str1 == str3); // false
    System.out.println(str3 == str5); // true
    System.out.println(str3.equals(str4)); // true
    System.out.println(str3.equals(str1)); // true
    System.out.println(str3.equals(str5)); // true
    System.out.println(str1.equals(str2)); // true
    
    
  • For the reference type ,“==” checks the address (or the object), while .equal() checks the value(or the content).

  • When we assign “abc” to str1 , java will not build a new string object immediately .Instead, java checks whether there exists “abc” in the stack constant field first. If thers is no “abc” , it will build a constant “abc” in the stack.

  • The assignment of str2 is after the str1 , so ‘abc’ has already existed in the constant field. str2 will point at the

  • the new keyword creates a new String object and returns the address of the overall object for assignment to str3 . Although the values of str1 ,str3 and str4 are both “abc” , the addresses are different.

  • Every time we use new keyword, java will create a completely different object and assign a new address to it , which means the address that java assigns to the new object is independent of what the object contains.

When we declare a variable of a certain type, java finds a contiguous block with exactly enought bits to hold a thing of that type. Each data type in java holds a different number of bits. Unlike C or python ,java can not access the exact address of the data . In java , the exact memory address is below the level of the abstraction accessible to us.

  • Local Variable
    • Local Variables can be declared in methods, code blocks, constructors, etc in java.
    • Local variables do not have any default values in java
    • Local variable can be used only after it has been assigned
    • declaring a variable without initialization : int num
    • Initializing the variable : int num = 100;
// error : variable might not have been initialized
public class test{
   
	public static void main(String[] args){
   
		// local variable must be initialized before using ,otherwise complier errors
		int a ;
		System.out.println(a);
	}
}
  • Declare a static global primitive type variable without initialization
// output : 0
public class test{
   
	// static declaration for global int variable is important owing to  the static main function
	static int a ;
	public static void main(String[] args){
   
		System.out.println(a);
	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值