[Java] Syntax basics

Source: Internet
Author: User

1. Constants

Final int MAX = 100;

2. Variables

Key words The number of occupied positions Range of values
Boolean 8 True/false
Byte 8 -2^7 ~ 2^7-1
Short 16 -2^15 ~ 2^15-1
Int 32 -2^31 ~ 2^31-1
Long 64 -2^63 ~ 2^63-1
Float 32 3.4*10^-38 ~ 3.4*10^38
Double 64 1.7*10^-38 ~ 1.7*10^38
Char 16 0~65536 all the represented values in Unicode code

3. Operators

Binary arithmetic operator +,-,*,/,%

Unary arithmetic operator + +,--

A unary operator that precedes the operand, which means "increment self-decrement and then participate in other operations"

A unary operator that is located after the operand, which means "increase self-decrement after other operations first"

eg

int a = 5;

int b = ++a; B=6

int c =--a; C=5

int d = a++; D=5

int e = a--; E=6

A=5

4. Relational operators

>,<,>=,<=,==,!=

Floating-point numbers cannot do = = operations, while application Fabs (A-B) < epsilon

5. Logical operators

&&,| |,!

6. Conditional operators

C=a>b?a:b

7. Statements

Select if () {}else{}

Cyclic for,while,do...while

Jump Break,continue,return

eg

Statement1;

tab:{

if (true)

Break tab;

Statement2; Does not execute

}

Statement3;

8. Output statements

System.out.println ();

System.out.print ();

eg

int a = 0;

System.out.println ("abc123");

System.out.println (a);

System.out.println ("abc123" +a);

9. Input statements

System.in.read (); Reads the ASCII code of a character, returning to the form of an integral type

System.in.read (byte b[]); Reads a line of characters into an array of byte b[], returning the number of character arrays

Mark There is a difference between the quasi-input and the standard output, which is that the input involves exception handling, so you must add an exception-handling statement. The simplest exception handling method is to add throws to the main () header Exception

eg

public static void Main (string[] args) throws Exception{

byte b[] = new BYTE[64];

int A;

A = System.in.read (b);

for (int i = 0; i < A; i++) {

System.out.println ((char) b[i]);

}

}

10. Other input methods

Scanner class. There are methods in the Scanner class Netint () to receive numbers, and Nextchar () is used to receive characters.

eg

Import java.util.*;

public class example{

public static void Main (string[] args) {

int A, B;

Scanner s = new Scanner (system.in);

A = S.nextint ();

b = S.netint ();

System.out.println (A+B);

}

}

[Java] Syntax basics

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.