Java Language Elements
Data Types
Java supports both primitive and non-primitive data types. Eight primitive data types (or simple types) include byte, short, int, long, char, float, double, and boolean.
- Integer - These are whole values numbers (positive or negative). They include:
-
byte The byte data type is an 8-bit integer, with range from −128 to 127. The byte data type is useful for saving memory in large arrays.
short The short data type is a 16-bit integer, with range from −32,768 to 32,767 . Similar to byte, short is used to save memory in large arrays.
int The int data type is an 32-bit integer, with range from −2,147,483,648 to 2,147,483,647. This is most commonly used data type.
long The long data type is 64-bit integer, with range from −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. It is useful when the range of values is higher then that which can be accommodated by int.
- Floating Point - These are numbers with fractional precision. They include:
-
float - This data type is a single precision number format that occupies 4 bytes or 32 bits in computer memory. For float data type, append the letter f to the end of the data type, otherwise it will be saved as double.
double - This data type is a double precision number format that occupies 8 bytes or 64 bits in computer memory. - Character - This include symbols in a character set, such as letters and numbers. It is represented by key word char.
- Boolean - This include true or false values. The default value is false.
Primitive data types is non-object oriented part of java. Non-primitive or reference data types which are defined by the programmer, are also supported by Java, such as Classes, Interfaces, Arrays, Strings etc.
Java is strongly typed language, i.e. every variable and expression has a data type which do not change, and there is range of values a variable can hold. Compiler check for type compatibility for all declared variables or parameters passed to various methods.
Java Operators Most of Java Operators can be divided into arithmetic, bitwise, relational, and logical groups.With the exception of the equivalence and nonequivalence operators, all other relational operators work with all primitives, except boolean.
The Conditional or logical operators AND (&&) and OR (||) also produce a boolean result of true or false based on the logical relationship of its arguments.
Variables A variable is basic unit of storage. Their type need to be declared before usage. A variable can be named using as identifier and by declaring its type. A variable can be optionally be given an initial value by using equal sign and a value.
An integer variable b can be declared as int b; Above variable can be initialized as int b =1; A double variable can be declared as: double pi = 3.14159 which give the approximate value of pi. Float specifies single precision value, which can be indicated for variable dollar as: float dollar; A character variable can be used to store characters. It can be declared as: char c = "a"; A boolean, can be declared as: boolean b; Here "b" can take value , either true or false, depending upon the condition. Classes and Objects
Class contain fields and methods, and is template of an object which is an instance of class. The data or variables defined in a class are called instance variables. Each object contain its own copy of the instance variables. Starting point of the program is main method. A java program can run without class with main method too if starting class is specified.
A class can be defined by the key word class. Various class forms can contain one or more additional key words such as public, private, package, default, static, abstract etc. Variables can be one of the primitive forms, such as int, boolean, char, etc. Methods are declared by using key word method . Additional key words can be used with method declaration depending on its usage, such as public, void, abstract, final etc. Parameters can be optionally passed to the method.
Object is declared by declaring variable of class type and assign an object to this variable using new key word, which allocates memory address and return reference for it, stored in the variable. The new operator dynamically allocates memory for new object. A constructor initializes the object as soon as it is created. It is declared in syntax similar to method, but with same name as that of class. The default constructor initializes all instance variables to zero. Parameters can be assigned to constructor, so as to give specific initial values to the variables. For objects which are no longer needed, java can destroy them by using a mechanism called garbage collection. These are the objects for which references no longer exist, so it allows to free memory used by them.
Basic I/O StatementsJava provides an extensive library of classes for managing all types of data. These classes are built around a stream model. Stream is a sequence of characters (in the case of textual data) or bytes (in the case of binary data) that can be accessed in order. It provides classes for input and output, such as interaction with the user, and reading and writing to files.
Packages
Group of classes and interfaces are combined as package, which provide various functionalities. Using a class or interface without importing the package it is located in, will give an error. Packages are imported using the import keyword, before any class declaration. To import the entire io java package we will use: import java.io.*.
Some of the common java import packages are:
java.lang - Provides classes that are fundamental to developing Java programs. Imported by default into all Java programs.
java.awt - Provides classes for creating graphical user interfaces, drawing graphics, and displaying images.
java.net - Provides classes for creating communication and exchanging data over a network.
java.text - Some formatting classes.
java.util.regex - Regular expression classes.
java.util - THis package contain various utility classes and interfaces. It contains the collections framework, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes.
Java Key Words
There are certain words with a specific meaning in java. These Keywords cannot be used as variable names, class names, or method names. Keywords in java are case sensitive, all characters being lower case.
abstract |
continue |
for |
new |
switch |
assert |
default |
goto |
package |
synchronized |
boolean |
do |
if |
private |
this |
break |
double |
implements |
protected |
throw |
byte |
else |
import |
public |
throws |
case |
enum |
instanceof |
return |
transient |
catch |
extends |
int |
short |
try |
char |
final |
interface |
static |
void |
class |
finally |
long |
strictfp |
volatile |
const |
float |
native |
super |
while |