jump to navigation

Language Fundamentals August 5, 2005

Posted by Coolguy in J2SE.


  • Identifiers are names of variables, functions, classes etc.
  • The name used as an identifier must follow the following rules in Java
  • Each character is either a digit, letter, underscore(_) or currency symbol ($,¢, £ or ¥)
  • First character cannot be a digit.
  • The identifier name must not be a reserved word


Keywords in the Java Language

assert ***

goto *










enum ****










strictfp **


const *


  • A literal in Java technology denotes a constant value.
  • So for example 0 is an integer literal, and ‘c’ is a character literal.
  • “This is a string” is a string literal
  • The reserved literals true and false are used to represent boolean literals.
  • Integer literals can also be specified as octal (base 8), or hexadecimal (base 16).
  • Octal and hexadecimal have 0 and 0x prefix respectively. So 03 and 0x3 are representation of integer three in octal and hexa-decimal respectively.


  • A single line comment starting with //
  • A multi-line comment enclosed between /* and */
  • A documentation or javadoc comment is enclosed between /** and */.

Primitive data types:

  • Boolean (for representing true or false),
  • Character type called char,
  • Four integer types (byte, short, int and long)
  • Two floating point types (float and double).

Data types Width (in bytes)

Byte = 8 bits

Minimum value Maximum Value
byte 1 -27-128 27 – 1 127
short 2 -215-32,768 215-1 32,767
int 4 -231 231 – 1
long 8 -263 263 – 1
char 2 0x0 0 0xffff 65,535
float 4 1.401298e-45 3.402823e+38
double 8 4.940656e-324 1.797693e+308

Wrapper class

  • Corresponding to all the primitive type there is a wrapper class defined.
  • These classes provide useful methods for manipulating primitive data values and objects


  • A variable is an item of data named by an identifier.
  • In the Java programming language, the following must hold true for a simple name:
  • It must be a legal identifier, It must not be a keyword, a boolean literal (true or false), or the reserved word null.
  • It must be unique within its scope.
  • Variable names begin with a lowercase letter, and class names begin with an uppercase letter. If a variable name consists of more than one word, the words are joined together, and each word after the first begins with an uppercase letter, like this: isVisible. The underscore character ( _ ) is acceptable anywhere in a name, but by convention is used only to separate words in constants (because constants are all caps by convention and thus cannot be case-delimited).
  • A variable’s scope is the region of a program within which the variable can be referred to by its simple name.
  • Scope also determines when the system creates and destroys memory for the variable
  • There are four categories of scope
  • Member variable
  • Local variables
  • Parameters
  • Exception-handler parameters

Member variable

  • A member variable is a member of a class or an object.
  • It is declared within a class but outside of any method or constructor.
  • A member variable’s scope is the entire declaration of the class

Local variables

  • Local variables are declared within a block of code.
  • In general, the scope of a local variable extends from its declaration to the end of the code block in which it was declared.


  • Parameters are formal arguments to methods or constructors and are used to pass values into methods and constructors.
  • The scope of a parameter is the entire method or constructor for which it is a parameter

Exception-handler parameters

  • Exception-handler parameters are similar to parameters but are arguments to an exception handler rather than to a method or a constructor.
  • The scope of an exception-handler parameter is the code block between { and } that follow a catch statement.

Initializtion of variables

  • Instance variables (data members of a class) and static variables are initialized to default values.
  • Local variables (i.e. variables defined in blocks or inside member functions) are not initialized to default values.
  • Local variables must be explicitly initialized before they are used.
  • If local variables are used before initialization, compilation error gets generated
  • The defaults for static and instance variables are given in the table below.

Data types Default Values
boolean false
char ‘\u0000’
Integer types (byte, short, int, long) 0
Floating types (float, double) 0.0F or 0.0 D
Object References null

Java source file

  • Empty file is a legal Java file.
  • When a Java program is invoked then the Java interpreter name “java” and the class name are not passed to the main() method of the class. The rest of the command line arguments are passed as an array of String.


  • An operator performs a function on one, two, or three operands.
    Types of operators:
  • Unary: An operator that requires one operand is called a unary operator
  • Binary: An operator that requires two operands is a binary operator
  • Ternary:A ternary operator is one that requires three operands. Eg: ?:

Conversion rules in Assignments

  • Basic conversion rules for assignment when source and destination are of different types.
  • If source and destination are of the same type, assignment happens without any issues.
  • If source is of smaller size than destination but source and destination are of compatible types, then no casting is required. Implicit widening takes place in this case. An example is assigning an int to a long
  • If source and destination are of compatible types, but source is of larger size than destination, explicit casting is required. In this case, if no casting is provided then the program does not compile.
Floating point numbers
  • Decimal numbers (for example 1.3) are of type double by default. To make them of type float they must be followed by F (say, 1.3F)
The equality operator
  • The equality operator (==) when applied to objects return true if two objects have same reference value, false otherwise.

The conditional operators && and

  • Operator && returns true if both operands are true, false otherwise
  • Operator returns false if both operands are false, true otherwise.
  • These operators are short-circuited. left operand is evaluated before the right operator

Storing integral types

  • All the integer types in Java technology are internally stored in two’s complement
  • In two’s complement, positive numbers have their corresponding binary representation
  • Negative number obtained by taking 1’s Complement of positive number and adding 1.
  • Eg: http://www.csci.csusb.edu/dick/cs599/binary.html

The shift operators

  • The shift left operator in Java technology is “<<“.
  • The left shift operator fills the right bits by zero. The effect of each left shift is multiplying the number by two.
  • There are two operators for doing the right shift – signed right shift (>>) and zero fill right shift (>>>)
  • Zero fill shift right is represented by the symbol >>>. This operator fills the leftmost bits by zeros. So the result of applying the operator >>> is always positive.
  • Signed right shift operator (>>) fills the left most bit by the sign bit.

Operator Precedence

  • If you don’t explicitly indicate the order in which you want the operations in a compound expression to be performed, the order is determined by the precedence assigned to the operators in use within the expression.
  • Operators with a higher precedence get evaluated first.
  • When operators of equal precedence appear in the same expression, all binary operators except for the assignment operators are evaluated from left to right. Assignment operators are evaluated right to left.

Operator Precedence
postfix operators expr++ expr--
unary operators ++expr --expr +expr -expr ~ !
multiplicative * / %
additive + -
shift << >> >>>
relational < > <= > = instanceof
equality == !=
bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR
logical AND &amp;amp;&
logical OR
conditional ? :
assignment = += -= *= /= %= &= ^= = << = >> = >>> =

Array Fundamentals

  • Memory is allocated for an array is done using the new operator
  • The elements of the array are implicitly initialized to default values based on array types.This is true for both local arrays as well as arrays which are data members. In this respect arrays are different from normal variables. Variable defined inside a method are not implicitly initialized, where as array elements are implicitly initialized.
  • The length operator can be used to access the number of elements in an array
  • When creating multi-dimensional arrays the initial index must be created before a later index


No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: