Professional Documents
Culture Documents
Java Tokens and Type Casting
Java Tokens and Type Casting
FUNDAMENTALS
JAVA TOKENS AND IDENTIFIERS
KEYWORDS AND LITERALS
SEPARATORS AND COMMENTS
JAVA STATEMENTS
DATA TYPES AND VARIABLES
SYMBOLIC CONSTANTS
READING INPUT AND WRITING DATA
TYPE CASTING
JAVA TOKENS
TOKENS ARE THE BASIC BULDING BLOCKS OF JAVA LANGUAGE
A token is the smallest element of a program that is meaningful to the compiler. Tokens can
be classified as follows:
Keywords
Identifiers
Constants or Literals
Special Symbols-Separators
Operators
IDENIFIERS
Identifiers are used as the general terminology for naming of variables, functions ,classes and
packages.
Identifiers are case sensitive
VALID IDENTIFIERS
MyVariable
MYVARIABLE
myvariable
X
I
x1
i1
_myvariable
$myvariable
sum_of_array
INVALID IDENTIFIERS
My Variable // contains a space
/*
between slash star and star slash
you can mash lines down real far, or
*/
/**
* javadoc comments for external documentation
* @return The square root of x
*/
public static double sqrt(double x)
JAVA STATEMENTS
A statement, made up of tokens, is code that causes something to happen while
the program runs
1. PACKAGE STATEMENT - package animal
2. IMPORT STATEMENT – import java.util.*;
3. LABELLED STATEMENT – outer : while(i<10)
4. EXPRESSION STATEMENT – int a = 3+2
5. SELECTION STATEMENT – if()…else ( )
6. ITERATION STATEMENT – while(i<10)
JAVA STATEMENTS
1. JUMP STATEMENTS – return , break;
2. SYNCHRONIZATION STATEMENT – synchronized(this)
3. EXCEPTION HANDLING STATEMENT - try { } . Catch { }
4. ASSIGNMENT STATEMENT – int a = 10;
DATA TYPES IN JAVA
There are majorly two types of languages.
First one is Statically typed language where each variable and expression type is already
known at compile time. Once a variable is declared to be of a certain data type, it cannot hold
values of other data types.
Example: C, C++, Java.
The other is Dynamically typed languages. These languages can receive different data types
over time.
Example: Ruby, Python
Java is statically typed and also a strongly typed language because in Java, each type of
data (such as integer, character, hexadecimal, packed decimal, and so forth) is predefined as
part of the programming language and all constants or variables defined for a given program
must be described with one of the data types.
DATA TYPES IN JAVA
Java has two categories of data:
Primitive Data Type: such as boolean, char, int, short, byte, long, float and double
Non-Primitive Data Type or Object Data type: such as String, Array, etc.
Data Type Default Value Default size
class Marks {
int engMarks; // These variables are instance variables.
int mathsMarks; // These variables are in a class
int phyMarks; // and are not inside any function
}
class MarksDemo {
public static void main(String args[])
{ // first object
Marks obj1 = new Marks();
obj1.engMarks = 50;
obj1.mathsMarks = 80;
obj1.phyMarks = 90;
// displaying marks for first object
System.out.println("Marks for first object:");
System.out.println(obj1.engMarks);
System.out.println(obj1.mathsMarks);
System.out.println(obj1.phyMarks);
}
}
SCOPE : CLASS/STATIC
VARIABLE
Static variables are also known as Class variables.These variables are declared similarly as instance
variables, the difference is that static variables are declared using the static keyword within a class outside
any method constructor or block.
Unlike instance variables, we can only have one copy of a static variable per class irrespective of how
many objects we create.
Static variables are created at the start of program execution and destroyed automatically when execution
ends.
Initilisation of Static Variable is not Mandatory. Its default value is 0
If we access the static variable like Instance variable (through an object), the compiler will show the
warning message and it won’t halt the program. The compiler will replace the object name to class name
automatically.
If we access the static variable without the class name, Compiler will automatically append the class name.
To access static variables, we need not create an object of that class, we can simply access the variable as
import java.io.*;
class Emp {
// static variable salary
public static double salary;
public static String name = "Harsh";
}
public class EmpDemo {
public static void main(String args[])
{ // accessing static variable without object
Emp.salary = 1000;
System.out.println(Emp.name + "'s average salary:"
+ Emp.salary);
}
}
DEFAULT VALUE OF
VARIABLE
import java.io.*;
class Emp {
Host Machine
Java Program
ASCII?
Using Unicode
EBCDIC?
Writer Class
char
OutputStream
Class
READING INPUT FROM KEYBOARD
Step 1 – InputStreamReader is = new InputStreamReader( System.in);
BufferedReader
adds readLine()
and buffering
InputStreamReader
System.in
(InputStream)
adds read()
an abstract class
READING INPUT FROM
KEYBOARD
Step 3 – Read the entire line from keyboard using BufferedReader class method
String str = br.readLine();
Step 4 – Read the Single character from keyboard using BufferedReader read method:
Char ch = (char)br.read(); where (char) is the type casting. The ASCII value converted to character
value.
Step 5 – Read the integer from keyboard
Int age = Integer.ParseInt(br.readLine());
Readline returns the string, Parseint converts it into the integer.
USING SCANNER CLASS
Scanner class is introduced from Java 1.5 onwards
Step 2:
Create and scanner object and attach it to System.in
Scanner sc = new Scanner(System.in);
Step 3:
Each input from the user on the keyboard is collected as tokens. If it is integer you can receive in sc.nextInt(), if it is string
you can received in sc.next() etc
String str = sc.next();
Int age = sc.nextInt();
Float avg = sc.nextFloat();
Double amount = sc.nextDouble();
Long bignumber = sc.Long();
Byte smallnumber = sc.nextByte();
WRITING DATA TO CONSOLE
System.out.print(‘A’);
System.out.print(“HELLO”);
System.out.print(250);
System.out.print(“The value of a is”+a);
System.out.println(“The value of a is” +a+ ”and b is” +b);
System.out.printf(“The value of a and b is %d,%d”, a,b);
TYPE CASTING
Type casting is when you assign a value of one primitive data type to another type.
In Java, there are two types of casting:
•Widening Casting (automatically IMPLICIT) - converting a smaller type to a larger type size
byte -> short -> char -> int -> long -> float -> double
•Narrowing Casting (manually EXPLICIT) - converting a larger type to a smaller size type
double -> float -> long -> int -> char -> short -> byte
WIDENING OR IMPLCIT
CASTING
Widening casting is done automatically when passing a smaller size type to a larger size type:
Short s=(short)I;
Char ch=(char)I;
Double d = 123.456;
Int I = (int)d;
Float f = (float)d;
Long l = (long)f;chr c = char(i);
Short sh = (short)ch;
Byte b = (byte)sh;