Primitive Data Type


Type



Name Wrapper class


byte java.lang.Byte −128 through +127 8-bit (1-byte)
short java.lang.Short −32,768 through +32,767 16-bit (2-byte)
int java.lang.Integer −2,147,483,648 through +2,147,483,647 32-bit (4-byte)
long java.lang.Long −9,223,372,036,854,775,808 through +9,223,372,036,854,775,807 64-bit (8-byte)
float java.lang.Float ±1.401298E−45 through ±3.402823E+38 32-bit (4-byte)
double java.lang.Double ±4.94065645841246E−324 through ±1.79769313486232E+308 64-bit (8-byte)
boolean java.lang.Boolean true or false 1-bit (1-bit)
char java.lang.Character '\u0000' through '\uFFFF' 16-bit (2-byte)

Extra Controlling by java

Enhanced for loop
Enhanced for loops have been available since J2SE 5.0. This type of loop uses built-in iterators over arrays
and collections to return each item in the given collection. Every element will be returned and reachable in
the context of the code block. When the block has been executed the next item will be returned until there
are no items remaining. Unlike C# this kind of loop does not involve a special keyword but instead uses a
different notation style.


for (int i : intArray) {
doSomething(i);
}


Jump statements

Labels

Labels are given points in code used by break and continue statements. Despite the presence of the goto
keyword, it cannot be used to jump to specific points in the code.

start:
someMethod();


break
It is possible to break out of the outer loop using labels:
outer:
for (int i = 0; i < 10; i++) {
while (true) {
break outer;
}
}

// Will break to this point


continue

outer:
for (String str : stringsArr) {
char[] strChars = str.toCharArray();
for (char ch : strChars) {
if (ch == ' ') {
/* Continues the outer cycle and the next
string is retrieved from stringsArr */
continue outer;
}
doSomething(ch);
}
}

Operators

Operators in Java are similar to those in C++. However, there is no delete operator due to garbage
collection mechanisms in Java, and there are no operations on pointers since Java does not support them.
Another difference is that Java has an unsigned right shift operator (>>>), while C's right shift operator's
signedness is type-dependent. Operators in Java cannot be overloaded.
>


Operator Description
() Method invocation
[] Array access
. Class member selection
++ -- Postfix increment and decrement
++ -- Prefix increment and decrement
+ - Unary plus and minus
! ~ Logical NOT and bitwise NOT
(type) val Type cast
new Class instance or array creation
* / % Multiplication, division, and modulus (remainder)
+ - Addition and subtraction
+ String concatenation
<< >> >>> Bitwise left shift, signed right shift and unsigned right shift
< <= Relational “less than” and “less than or equal to”
> >= Relational “greater than” and “greater than or equal to”
instanceof Type comparison
== != Relational “equal to” and “not equal to”
& Bitwise and logical AND
^ Bitwise and logical XOR (exclusive or)
| Bitwise and logical OR (inclusive or)
&& Logical conditional-AND
|| Logical conditional-OR
c ? t : Ternary conditional (see ?:)
= Simple assignment
+= -= Assignment by sum and difference
*= /= %= Assignment by product, quotient, and remainder
<<= >>= Assignment by bitwise left shift, signed right shift and unsigned
>>>= right shift
&= ^= |= Assignment by bitwise AND, XOR, and OR

Program structure

A Java application consists of classes and their members. Classes exist in packages but can also be nested
inside other classes.

 
main() method

Whether it is a console or a graphical interface application the program must have an entrypoint of some
sort. The entrypoint of the Java application is the main method. There can be more than one class with main
method, but the main class is always defined externally (e.g. in a manifest file). The method must be static
and is passed command-line arguments as an array of strings. Unlike C++ or C# it never returns a value and
must return void.

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

package

Packages are a part of a class name and they are used to group and/or distinguish named entities from other
ones. Another purpose of packages is to govern code access together with access modifiers. For example,
java.io.InputStream is a fully qualified class name for the class InputStream which is located in the
package java.io.
A package is declared at the start of the file with the package declaration:

package myapplication.mylibrary;
public class MyClass {
}

Classes with the public modifier must be placed in the files with the same name and java extension and put
into nested folders corresponding to the package name. The above class myapplication.mylibrary.MyClass
will have the following path: "myapplication/mylibrary/MyClass.java".

Import declaration
Type import declaration

A type import declaration allows a named type to be referred to by a simple name rather than the full name
including the package. Import declarations can be single type import declarations or import-on-demand
declarations. Import declarations must be placed at the top of a code file after the package declaration.


import java.util.Random; // Single type declaration
public class ImportsTest {
public static void main() {
/* The following line is equivalent to
* java.util.Random random = new java.util.Random();
* It would've been incorrect without the import declaration */
Random random = new Random();
}
}

Import-on-demand declarations allow to import all the types of the package, in the case of type import, or
members, in the case of static import, as they are mentioned in the code.

Static import declaration

This type of declaration has been available since J2SE 5.0. Static import declarations allow access to static
members defined in another class, interface, annotation, or enum; without specifying the class name:

import static java.lang.System.out; //'out' is a static field in java.lang.System
public class HelloWorld {
public static void main(String[] args) {
/* The following line is equivalent to:
System.out.println("Hello World!");
and would have been incorrect without the import declaration. */
out.println("Hello World!");
}
}

Import-on-demand declarations allow to import all the fields of the type:

import static java.lang.System.*;
/* This form of declaration makes all
fields in the java.lang.System class available by name, and may be used instead
of the import declaration in the previous example. */


Enum constants may also be used with static import. For example, this enum is in the package called 
screen:

public enum ColorName {
RED, BLUE, GREEN
};

It is possible to use static import declarations in another class to retrieve the enum constants:
import screen.ColorName;
import static screen.ColorName.*;
public class Dots {
/* The following line is equivalent to 'ColorName foo = ColorName.RED',
and it would have been incorrect without the static import. */
ColorName foo = RED;
void shift() {
/* The following line is equivalent to:
if (foo == ColorName.RED) foo = ColorName.BLUE; */
if (foo == RED) foo = BLUE;
}
}


Literals

Integers
0b11110101 (0b followed by a binary number)
0365 (0 followed by an octal number)
0xF5 (0x followed by a hexadecimal number)
245 (decimal number)

Floating-point values
23.5F, .5f, 1.72E3F (decimal fraction with an optional exponent indicator,
followed by F)
0x.5FP0F, 0x.5P-6f (0x followed by a hexadecimal fraction with a mandatory
exponent indicator and a suffix F)
23.5D, .5, 1.72E3D (decimal fraction with an optional exponent indicator,
followed by optional D)
0x.5FP0, 0x.5P-6D (0x followed by a hexadecimal fraction with a mandatory
exponent indicator and an optional suffix D)

Character literals
'a', 'Z', '\u0231' (character or a character escape, enclosed in single quotes)

Boolean literals
true, false

null literal
null

String literals
"Hello, World" (sequence of characters and character escapes enclosed in
double quotes)

Characters escapes in strings
\u3876 (\u followed by the hexadecimal unicode code point up to U+FFFF)
\352 (octal number not exceeding 377, preceded by backslash)
\n
\r
\f
\\
\'
\"
\t
\b

Integer literals are of int type by default unless long type is specified by appending L or l suffix to the
literal, e.g. 367L. Since Java SE 7 it is possible to include underscores between numbers to increase
readbility, for example a number 145608987 may be written as 145_608_987.
Variables

Java Keyword

abstract
assert[Note 1]
boolean
break
byte
case
catch
char
class
const[Note 2]
continue
default
do
double
else
enum[Note]
extends
final
finally
float
for
goto[Note
if
implements
import
instanceof
int
interface
long
native
new
package
private
protected
public
return
short
static
strictfp[Note 4]
super
switch
synchronized
this
throw
throws
transient
try
void
volatile
while

Identifier

An identifier is the name of an element in the code. There are certain standard naming conventions to follow
when selecting names for elements. Identifiers in Java are case-sensitive.


An identifier can contain:

Any Unicode character that is a letter (including numeric letters like Roman numerals) or digit.
Currency sign (such as $).

Connecting punctuation character (such as _).
An identifier cannot:
  1. start with a digit.
  2. be equal to a reserved keyword, null literal or boolean literal.