You are on page 1of 5

Java Guide.

Identifiers.

Examples of valid identifiers are as follows:

 num1 // Can use a-z and 0-9 and start with a letter
 kn // Only letters
 _abc // Can start with an underscore
 _ // Can have only one letter, which is an underscore
 sum_of_two_numbers // Can have letters and underscores
 Outer$Inner // Can have a-z, A-Z and $
 $var // Can also start with $

Examples of invalid identifiers are as follows:

 2num // Cannot start with a number


 my name // Cannot have a space
 num1+num2 // Cannot have + sign
 num1-num2 / Cannot have hyphen or minus sign

Underscores in numeric literals.


Beginning with Java 7, you can use any number of underscores between two digits in numeric literals.
For example, an int literal 1969 can be written as 1_969, 19_69, 196_9, 1___969, or any other forms
using underscores between two digits. Underscores are allowed in numeric literals only between digits.
This means that you cannot use underscores in the beginning or end of a numeric literal.

 int x1 = 1_969; // Underscore in deciaml format


 int x2 = 1__969; // Multiple consecutive underscores
 int x3 = 03_661; // Underscore in octal literal
 int x4 = 0b0111_1011_0001; // Underscore in binary literal
 int x5 = 0x7_B_1; // Underscores in hexadecimal literal
 byte b1 = 1_2_7; // Underscores in decimal format
 double d1 = 1_969.09_19; // Underscores in double literal

Increment and decrement operator


There are two kinds of increment operators:

• Post-fix increment operator, for example, i++


• Pre-fix increment operator, for example, ++i

When ++ appears after its operand, it is called a post-fix increment operator. When ++ appears before
its operand, it is called a pre-fix increment operator. The only difference in post-fix and pre-fix increment
operators is the order in which it uses the current value of its operand and the increment in its
operand’s value. The post-fix increment uses the current value of its operand first, and then increments
the operand’s value, as you saw in the expression.

Example.

a)

int i = 100;

int j = 50;

j = ++i + 15; // i becomes 101 and assigns 116 to j

b)

int i = 100;

int j = 50;

j = i++ + 15; // Assigns 115 to j and i becomes 101

Java Precedence (Jararquia de operadores)


If operators have the same precedence, the expression is evaluated from left to right.
Bitwise Operators

LOOPS
Initialization

Expression-list

The expression-list
part is optional. It may
contain one or more
expressions separated
by a comma. You can
use only expressions
that can be converted
to a statement by
appending a semicolon
at the end.
for(int num = 1; num <=
10;
System.out.println(num),
num++);

Static Import Declarations


A static import declaration also comes in two flavors: single-static import and static-import-on-demand.
A singlestatic import declaration imports one static member from a type. A static-import-on-demand
declaration imports all static members of a type. The general syntax of static import declaration is as
follows:

Single-static-import declaration:

• import static <<package name>>.<<type name>>.<<static member name>>;


Static-import-on-demand declaration:

• import static <<package name>>.<<type name>>.*;

Your program now does not need to qualify the out variable with the System class name as System.out.
Rather, it can use the name out to mean System.out in your program. The compiler will use the static
import declaration to resolve the name out to System.out.

Using Static Imports to Import Multiple Static Members of a Type

The Special main( ) Method


Can you have more than one main() method in a class? The answer is yes. You can have multiple
methods in a class, which can be named main as long as they do not have the same signature. The
following declaration for the

MultipleMainMethod class, which declares three main() methods, is valid. The first main() method,
which is declared as public static void main(String[] args), may be used as the entry point to the Test
class. The other two main() methods have no special significance as far as the JVM is concerned.
Can you invoke the main() method in your code? Or, can it be invoked only by the JVM? The main()
method is invoked by JVM when you run a class. Apart from that, you can treat the main() method as
any other class method.

Calling a Constructor from another Constructor

You might also like