Preparing Java Blue Bridge cup (2): The basic grammar Part: java programming specifications and common data types Summary

1. Oriented Programming java

  •       java is a pure object-oriented languages, all programs should be defined in the class, the class can contain only variable member, member function, and a code block.
  •       Other execution of the statement can not appear directly in the class body.
  •       java programs are started from and C / C ++ as the main function
  •       java in a java file only at most contain a public class    and when a public clas main function class in the class definition of only
  •       xxx.java file filename must be the same as the class name of the class where the main function   

      Java manner defined in the main function of 

public static void main(String[] args){....}

  The sample program: Fibonacci number

import java.util.*;
public class Main {
	public static int arr[] = new int[1000005];
	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		arr[1] = arr[2] = 1;
		for(int i = 3; i <= 1000004; i++){
			arr[i] = (arr[i-1] + arr[i-2])%10007;
		}
		System.out.print(arr[n]);
	}
}

2. The common data types:

int  float double  long char boolean byte

Define how the form: int a; float b; double c; boolean f = true

 

Array :

Define how the form:

  • int arr[] = new int[100];
  • int arr[] = {1,2,3,4};
  • Or int arr []; arr = new int [100];
  • These three ways [] can be placed before the variable name. Accustomed to C can put behind.

 

String created (note the capital S!):

  • 1.String defined method of the form: String S1 = "QWE";
  • 2. Because String is a class, it is also possible to initialize the constructor, the form String s2 = new String ( "asd "); 
  • Note that the string object is created by the first method, memory allocation in "constant pool"  in. In the constant pool of data is shared,  when there qwe this constant, if the String s2 = "qwe"; when, at this time JVM does not create a new string object, but let s2 point to address this constant qwe , that is to say s1 and s2 are the same memory address.
  • For the second way, each creates a string object will re-open a space for your string constants in the heap.

String type of comparison:

   1.  ==, != :

    ==,! = Judged that two strings addresses are equal.

    As already mentioned, two objects directly = string assignment, if the address is the same as the same string constant.

    and so:
   

  String s1 = "qwe"; 
  String s2 = "qwe";   
  s1 == s2   // true

  String s1 = new String("qwe"); 
  String s2 = new String("qwe");   
  s1 == s2 // false 

    

  2. str1.equals(String str2); 

     equals () method is to directly compare the contents of two string object,

 String s1 = new String("qwe"); 
 String s2 = new String("qwe");   
 s1 == s2 // true 

   So equals method to use when comparing strings == not directly use . Remember

String object traversal:

Not directly through an index or index traversal in java String, but must first be converted to an array of characters.

String s1 = "123456";
char[] s2 = s1.toCharArray();
for(int i = 0; i < s2.length; i++){ 
    System.out.println(s2[i]);
}

  Note that no parentheses behind s2.length, because the length of the char array is a property, not methods.

 

Published 52 original articles · won praise 114 · Views 5998

Guess you like

Origin blog.csdn.net/GD_ONE/article/details/103559733