Java
Core Java
Language Fundamentals
Content
Identifiers
Reserved Keywords
Datatypes
Literals
Arrays
Types of variables
Var-arg methods
Main-methods
Command-Line arguments
Java coding standards
1. Identifiers
A name in Java program is known as an identifier which can be used for identification purpose. It can be a method name, class name, variable name, or label name.
Rules for defining Java identifiers
Allowed Java identifiers are
A-Z
a-z
0-9
$
_
If we are using any other character we get a compile-time error. Example: total_value is Valid, total# is Invalid
2. Identifiers can't start with a digit. Example: total123 is valid, 123total is Invalid
3. Java identifiers are case-sensitive. Java language itself is treated as a case-sensitive language. Example: int Number, int number, int Number -> All are valid
4. The identifiers can be of any length. There is no limit. But it's not recommended to take too lengthy identifiers.
5. We can't use reserved words as identifiers. Example: int x = 10 is valid, int if = 10 is invalid
6. All predefined Java class names and interface names can be used as identifiers. int String = 10; int Runnable = 20; Both are valid. Even though it is valid, it's not a good practice as it creates confusion.
Which of the following are valid Java identifiers?
total_value -> valid
total# -> invalid
123total -> invalid
ca$h -> valid
_$ -> valid
Java2Share -> valid
Integer -> valid
Int -> valid
int -> invalid
2. Reserved words
In Java, some words are reserved to represent some meaning or functionality. Such types of words are called reserved words.
Keywords for datatypes
byte, short, int, long, float, double, boolean, char
-> Total 8
keywords for flow-control
if, else, switch, case, default, while, do, for, break, continue, return
- Total 11
Keywords for modifiers
public, private, protected, static, final, abstract, synchronized, native, strictfp, transient, volatile
- Total 11
Keywords for exception-handling
try, catch, finally, throw, throws, assert
- Total 6
Keywords for class-related
class, extends, implements, interface, package, import
- Total 6
keywords for Object-related
new, instanceof, super, this
- Total 4
Keyword for grouped constants
enum
- Total 1
We can use an enum to define grouped constants.
Keywords for return type
void
- Total 1
In Java, the return
type is mandatory. If a method won't return anything then we need to declare with void
return type.
Banned keywords
GOTO: Usage of goto caused several problems so SUN banned this keyword.
CONST: There is
final
keyword
that can be used instead ofconst
We get a compile-time error using these keywords
Reserved Literals
true, false
- for boolean datatypesnull
- the default value for object reference
Conclusions:
All 53 reserved words in Java contain only lower case alphabet symbols.
In Java, we have the
new
keyword and notdelete
keyword because the destruction of useless objects is the responsibility of the Garbage collector
Basics
Datatypes
Negative numbers in java are represented by 2's complement form
2's complement means, first negate the bits and then add 1 to the number. Result is the 2s complement.
Then to convert the number back to it's original, take 2s complement on the result again
See below example of converting -10
Performing right shift on negative numbers (Left shift and unsigned right is covered below)
If the number is a negative number then, after taking 2's complement and shifted the number to right by any number of positions, the signed bit is always untouched.
The default decimal value in java is double and default integral is integer
Java Architecture
Operators
JVM architecture
When we left shift a number 10 (1010 in binary) by 1, then we get 20. The number got multiplied by 2 on shifting 1 left. On shifting 2 positions left, it multiplies by 4 (2**2). So to multiply by 2* *k then we shift by k to the left.
If we need to divide instead of multiplying we shift it by right. But we should not touch the first digit (signed bit or MSB) which represents the sign of the number. For unsigned right shift operator we can touch MSB and treat it as a normal bit and perform the right shift.
So if we do right shift just like left shift then that is unsigned right shift.
Swap two numbers without a temp or 3rrd variable
Can be done using XOR operation
In normal way it can be done like this
Printing
Strings
Methods of defining a string
Creating a string from string literal
Last updated