Basic java syntax and java syntax

Source: Internet
Author: User

Basic java syntax and java syntax
1 rule

1. Naming rules
A: The first letter of the class name should be in upper case, and the name should be MaLin HelloWorld.
B: The naming identifier is a combination of numbers, letters, underscores, and $. It cannot start with a number, or be a keyword or reserved word. Example: 3max 3d goto
C: variable name: the first letter must be a lowercase constant: must be greater than PI MAX
D: Method Name: The first letter must be in lowercase.
2. Case Sensitive
3. end with a semicolon ';'.

2. Data Types

A: Basic Data Types (four types and eight types)
1.Simple Type 
 
2.Encapsulation type(Simple data type conversion)
Byte Short Integer Long Float Double Character Boolean 
1. The basic type can only be passed by value, and the encapsulation class corresponding to each basic type is passed by reference.
2. In terms of performance, the basic types in java are created on the stack, and all object types are created on the stack (Object references are created on the stack)
B:Reference Type 
1. array 2. Class (object) 3. Interface

Note:
Machine language (binary language): consists of 1 and 0. Example: 00000101
Minimum unit: bit 8bit = 1 byte (byte, character) 'A '~ 'Z' 'A '~ 'Z' '0 '~ '9' special symbol: one Chinese Character = 2 bytes = 16 bits 'hao'
1024 byte = 1KB 1024kb = 1 mb 1024 mb = 1 gb 1024 gb = 1 tb
Package: Essential folder directory
Java. lang. * 1. Default java Import
Java. util. *; 1. Tool class
Java. SQL. *; 1. Database
Java.net. *; 1. Network Programming
Java. io. *; 1. input and output streams
Package shortcut key ctrl + shift + o

package com.companyname.test;import java.util.Scanner;public class Test {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);    }}
Three Variables and constants

1 syntax:
Data type variable name [= value];

Char sex; // declare the variable sex = 'male'; // initialize and assign a value to int age = 10; float score = 93.2F; int x, y, z = 20;

Final data type variable name [= value]; final int X = 1;
1. Declare 2. initialize 3. Use

Conversions between two types: 
2.1 small --> large: automatic type conversion

short  y = 1;System.out.println(y);double x =  y;

System. out. println (x);-> small: Forced type conversion

At score = 13; // 13 (int) --> small, to System. out. println (score); int rs = (int) score; // 0.3 truncated missing System. out. println (rs );

Four string types

String in java. lang. String
1. Must know: =, equals () string comparison difference:

Public class Test1 {public static void main (String [] args) {// String: Special java class int num = 100; String name = "jackson "; string address = "North Street West warehouse .... & quot; String phone = "18182679850"; System. out. println (name + address); // String concatenation System. out. println (3.14f + name); // any type + String = string System. out. println ("520" + "Michael"); String temp = "jackson"; String str1 = new String ("jackson"); // memory occupied by System. out. println (str1 );}}

2. Familiar with simple string types 
String. valueOf (parameter) any type + ""
Simple string type (encapsulation type: parseXxx ())

public class Test2 {    public static void main(String[] args) {        float num = 3.1f;        String str = String.valueOf(num);        System.out.println(str);//3.1(String)        num =  Float.parseFloat(str) ;    }}

3. Master: common functions in the String class 
 
 
 
Frequent String operations: the String class is not recommended, but StringBuilder & StringBuffer

Five Operators

1 Arithmetic Operator 
Binocular: */% (remainder, modulo) +-
Single Camera: ++-
A prefix (num ++ ;)

int num = 4;        ++num;        int rs = ++num*2;        System.out.println(rs);//?

Suffix B

int num = 4;        int x = 3;        num++;//5        int rs = num++ +x; //rs = 8  num=6        System.out.println(rs++);//?        System.out.println(rs);

2 Relational operators: True/false

<>=<== (Constant equals )! =
Logical operators: true/false
! (Reverse) & (short and) | (short or)

int x=3,y=4,z=10;boolean rs = x>y&&++z>10;// true / falseSystem.out.println(z);

Operator =
3 value assignment operator 
Value assignment operator: =
Compound assignment operator: + =-= * =/= % =

int x = 5;//      x = x + 10;        x+=10;        System.out.println(x);

4. Three-object Operator:
Exp1? Exp2: exp3;
Exp1: true exp2;
Exp1: false exp3;
5-bit Operators:
~ Non | or & is different from ^ or
By bit ~ Operator: 10100101 = 01011010
Bitwise & operator: 1101 & 1010 = 1000
Bitwise | Operator: 1101 | 1010 = 1111
Bitwise OR operator: 1101 ^ 1010 = 0111
6 Displacement Operators 
<3 <3 = 24>
If the number does not overflow, for positive and negative numbers, moving one to the left is equivalent to multiplying the 1 power of 2, and moving n to the left is equivalent to multiplying the n power of 2.
7 escape characters:
\ N line feed
\ R press ENTER
\ T horizontal tabulation
\ V vertical tabulation
\ ""
\''
\\

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.