Java Fundamentals

Basic Programming Concepts in Java:

▪Encoding Sets.
Java supports the Unicode character encoding set and program element names are not restricted to ASCII (American Standard Code for Information Interchange) characters. Text can be written using characters in practically any human language in use today.

▪Primitives.
Java is an OOP language and Java programs deal with objects most of the time. However, there are also non-object elements that represent numbers and simple values such as true and false. These simple non-object programming elements are called primitives.

▪Variables.
Variables are place holders whose contents can change. There are many types of variables.

▪Constants.
Place holders whose values cannot be changed.

▪Literals.
Literals are representations of data values that are understood by the Java compiler.

▪Primitive conversion.
Changing the type of a primitive to another type.

▪Operators.
Operators are notations indicating that certain operations are to be performed.

ASCII & Unicode:

-ASCII character set supports Latin Letters.Each character is represented by 8 bits.However all
not every language uses Latin letters.Chinese,Japanese,Thai are examples of languages which use different character set.8 bit is not enough to represent all the characters in the character set.

-Unicode Character set supports all characters in all languages in the world into one single character set.

-Initially Unicode character was represented by 16 bits, which were enough to store 65000 different characters.6500 were enough to encoding most of the characters in major languages in the world.However the Unicode consortium as extended it to 32 bit.

-While Unicode provides enough space for all character used in all languages,storing and transmitting the Unicode text is not as efficient as ASCII characters.

-Character encoding can make it more efficient to store and transmit the Unicode text. And, there are many types of character encoding's available today.

-The Unicode Consortium endorses three of them:

▪UTF-8.
This is popular for HTML and for protocols whereby Unicode characters are transformed into a variable length encoding of bytes. It has the advantages that the Unicode characters corresponding to the familiar ASCII set have the same byte values as ASCII, and that Unicode characters transformed into UTF-8 can be used with much existing software. Most browsers support the UTF-8 character encoding.

▪UTF-16.
In this character encoding, all the more commonly used characters fit into a single 16- bit code unit, and other less often used characters are accessible via pairs of 16-bit code units.

▪UTF-32.
This character encoding uses 32 bits for every single character. This is clearly not a choice for Internet applications. At least, not at present.

-ASCII characters still play a dominant role in software programming.

-Java too uses ASCII for almost all input elements, except comments, identifiers,
and the contents of characters and strings. For the latter, Java supports Unicode characters.

Primitives:

There are eight primitive types in Java, each with a specific format and size.
1. byte
- Byte length integer ( 8 bits )
-range is from -128 (-27) to 127 (27-1)
2.short
- Short integer (16 bits)
- range is from -32,768 (-215) to 32,767 (-215-1)
3.int
-Integer (32 bits)
- range is from -2,147,483,648 (-231) to 2,147,483,647 (-231-1)
4.float
-Single-precision floating point (32-bit IEEE 7541)
- range is from Smallest positive nonzero: 14e-45 Largest positive nonzero: 3.4028234e38
5.double
-Double-precision floating point (64-bit IEEE 754)
- range is from Smallest positive nonzero: 4.9e-324
Largest positive nonzero: 1.7976931348623157e308
6.char
- A Unicode character
7.boolean
- A boolean value true or false

Variables:

-Variables are data placeholders. Java is a strongly typed language, therefore every variable must have a declared type.
- There are two data types in Java:
▪ reference types. A variable of reference type provides a reference to an object.
▪primitive types. A variable of primitive type holds a primitive.

-In addition to the data type, a Java variable also has a name or an identifier.

-There are a few ground rules in choosing identifiers.
1. An identifier is an unlimited-length sequence of Java letters and Java digits. An identifier must begin with a Java letter.
2. An identifier must not be a Java keyword a boolean literal, or the null literal.
3. It must be unique within its scope.

- Identifier should not start with a number and should not include signs like +
ex : 4a is Invalid.
kite+ride is Invalid

-In Java names are case sensitive. c1 and C1 are two different identifier.

Sun’s Naming Convention for Variables:

Variable names should be short yet meaningful. They should be in mixed case with a lowercase first letter. Subsequent words start with capital letters. Variable names should not start with underscore _ or dollar sign $ characters.
For example, here are some examples of variable names that are in compliance with Sun’s code conventions: userName,countNumber,firstTimeLogin.

Constants:
In Java constants are variables whose values, once assigned, cannot be changed. You declare a constant by using the keyword final. By convention, constant names are all in upper case with words separated by underscores.

-Here are examples of constants or final variables.
final int ROW_COUNT = 50;
final boolean ALLOW_USER_ACCESS = true;

Literals:
Literals of primitive types have four subtypes:

integer literals, floating-point literals, character literals, and boolean literals.

Integer Literals:

-Integer literals may be written in decimal (base 10, something we are used to), hexadecimal (base 16), or octal (base 8).
Example: int x = 10;

-Hexadecimal integers are written by using the prefixes 0x or 0X. For example, the hexadecimal number 9E is written as 0X9E or 0x9E.

-Octal integers are written by prefixing the numbers with 0. For instance, the following is an octal number 567:
0567

-To assign a value to a long, suffix the number with the letter L or l.

-Longs, ints, shorts, and bytes can also be expressed in binaries by prefixing
the binaries with 0B or 0b. For instance:
byte twelve = 0B1100; // = 12

-If an integer literal is too long, starting from Java 7 you can use underscores to separate digits in integer literals.
ex:int million = 1_000_000;

Floating-Point Literals:

To express float literals, you use one of the following formats.
Digits . [Digits] [ExponentPart] f_or_F
. Digits [ExponentPart] f_or_F
Digits ExponentPart f_or_F
Digits [ExponentPart] f_or_F

To write double literals, use one of these formats.
Digits . [Digits] [ExponentPart] [d_or_D]
. Digits [ExponentPart] [d_or_D]
Digits ExponentPart [d_or_D]
Digits [ExponentPart] [d_or_D]

Example:
0f
3.14f
9.0001e+12f
Here are examples of double literals:
2e1
8.
.5
0.0D
3.14
9e-9d
7e123D

Boolean Literals:
-The boolean type has two values, represented by literals true and false. For example, the following code declares a boolean variable includeSign and assigns it the value of true.
boolean includeSign = true;

Character Literals:
-A character literal is a Unicode character or an escape sequence enclosed in single quotes.

Primitive Conversions:
-When dealing with different data types, you often need to perform conversions.

Identity Conversion:
If both variables have the same type, the assignment will always succeed. Conversion from a type to the same type is called identity conversion.
Ex:
int a = 90;
int b = a;

The Widening Conversion:
-The widening primitive conversion occurs from a type to another type whose size is the same or larger than that of the first type, such as from int (32 bits) to long (64 bits).

- The widening conversion is permitted in the following cases:

▪byte to short, int, long, float, or double
▪ short to int, long, float, or double
▪ char to int, long, float, or double
▪ int to long, float, or double
▪ long to float or double
▪ float to double

-A conversion from an int or a long to a float may result in loss of precision.

-The widening primitive conversion occurs implicitly.

Example :
int a = 10;
long b = a; // widening conversion

The Narrowing Conversion

-The narrowing conversion occurs from a type to a different type that has a smaller size, such as from a long (64 bits) to an int (32 bits).

- In general, the narrowing primitive conversion can occur in these cases:

▪ short to byte or char
▪ char to byte or short
▪ int to byte, short, or char
▪ long to byte, short, or char
▪ float to byte, short, char, int, or long
▪double to byte, short, char, int, long, or float

-Unlike the widening primitive conversion, the narrowing primitive conversion must be explicit. You need to specify the target type in parentheses.
For example, here is a narrowing conversion from long to int.

long a = 10;
int b = (int) a; // narrowing conversion

Example :
long a = 9876543210L;
int b = (int) a; // the value of b is now 1286608618

-A narrowing conversion that results in information loss introduces a defect in your program.


Operators:

In Java, there are six categories of operators.
▪Unary operators
▪Arithmetic operators
▪Relational and conditional operators
▪Shift and logical operators
▪Assignment operators
▪Other operators

Bitwise Complement Operator ~

-The operand of this operator must be an integer primitive or a variable of an integer primitive type. The result is the bitwise complement of the operand.

-we need to convert the operand to a binary number and reverse all the bits

For example:
int j = 2;
int k = ~j; // k = -3; j = 2

Shift Operators

-A shift operator takes two operands whose type must be convertible to an
integer primitive. The left-hand operand is the value to be shifted, the right-
hand operand indicates the shift distance.

- There are three types of shift
▪ the left shift operator <<
▪ the right shift operator >>
▪ the unsigned right shift operator >>>

The Left Shift Operator <<

-The left shift operator bit-shifts a number to the left, padding the right bits with 0. The value of n << s is n left-shifted s bit positions. This is the same as multiplication by two to the power of s.

For example, left-shifting an int whose value is 1 with a shift distance of 3 (1 << 3) results in 8

-Another rule is this. If the left-hand operand is an int, only the first five bits of the shift distance will be used. In other words, the shift distance must be within the range 0 and 31. If you pass an number greater than 31, only the first five bits will be used. This is to say, if x is an int, x << 32 is the same as x << 0; x << 33 is the same as x << 1.

-If the left-hand operand is a long, only the first six bits of the shift distance will be used. In other words, the shift distance actually used is within the range 0 and 63.

The Right Shift Operator >>

-The right shift operator >> bit-shifts the left-hand operand to the right. The value of n >> s is n right-shifted s bit positions. The resulting value is n/2s.
As an example, 16 >> 1 is equal to 8

The Unsigned Right Shift Operator >>>

-The value of n >>> s depends on whether n is positive or negative. For a positive n, the value is the same as n >> s.

-If n is negative, the value depends on the type of n. If n is an int, the value is (n>>s)+(2<<~s). If n is a long, the value is (n>>s)+(2L<<~s).

Promotion:
-For unary operators, if the type of the operand is byte, short, or char, the outcome is promoted to int.

-For binary operators, the promotion rules are as follows.
▪ If any of the operands is of type byte or short, then both operands will be converted to int and the outcome will be an int.
▪ If any of the operands is of type double, then the other operand is converted to double and the outcome will be a double.
▪ If any of the operands is of type float, then the other operand is converted to float and the outcome will be a float.
▪ If any of the operands is of type long, then the other operand is converted to long and the outcome will be a long.

ref:




Java Book:Java™: A Beginner's Tutorial-Budi Kurniawan