Java Basics
Chapter 07
Introduction
▪ Objected Oriented Programming Language.
▪ Developed by Sun Microsystem 1991.
▪ It is modelled after C++.
▪ Designed to be small, simple and portable across platforms and operating system both
at the source and at the binary level.
▪ It is called platform independent.
▪ Java Development Kit (JDK) has classes to support networking, common interpreter
protocols and user interface toolkit functions.
▪ Java Virtual Machine (JVM) is virtual computer where java programs are compiled into
machine language. This is called Java Bytecode.
▪ Bytecode slow execution speed.
The scenario of platform
independence
• Create java source file.
Java • Save file with java extension. Always file name and class
Sour name should be same.
ce
• Compile the source file using java compiler.
Java
Co • Javac is command used to compile the source code.
mpil
er
• After compilation java byte code file is generated.
Java • It has extension of .class
Byt • Java command is used to execute or run the bytecode file
eco
de
Example of Java Program
/** Documentation
* Program to print Student Roll number, Name, Standard, Stream and Marks.
*/ Comment
class student Class name
{
public static void main(String args[])
{
/* This section declares Multi line Comment
The variables used in the program */
int rollno, standard;
String name, stream;
float marks;
// Assign value to variables Single line Comment
rollno = 15;
name = “Varun”;
standard = 12;
stream = “Commerce”;
marks = 0.0;
// Display results
[Link](“Roll Number = “ + rollno);
[Link](“Name = “);
[Link](name);
[Link](“You are in “ + standard + “ and selected “ +
stream);
[Link](“Initial Marks = “ + marks);
} // end of main() function
} // end of class
Compilation Process
Java Source File (usually have
extension .java)
Java Compiler
(javac)
Java Bytecode file
(*.class)
JVM – Java
Interpreter
Structure of Java Program
▪ Syntax: The rules that specify
the basic vocabulary of the
language and how program is
constructed using variables,
expressions, statements,
branches, loops and methods.
▪ The definition of the method /
function consist
– Function header
– Sequence of statements enclosed
within curly braces. { and }
▪ Java is a free-format language.
Java Primitive Data types
Primitive Types
Integers Floating-point Characters Boolean
Real
Signed and Unsigned Unicode Character True or False
Numbers
Byte Short Int long Float Double Char boolean
1 byte 2 bytes 4 bytes 8 bytes 4 bytes 8 bytes 2 bytes 1 byte
Signed values in range of -2b-1, 2b-1 Real numbers are
Unsigned values in range of 0, 2b-1 compliant with IEEE 754
Variable
▪ A name used to refer to the data stored in memory is called variable.
▪ It can take different data values at different times during program
execution, but it always refers to the same memory location.
▪ It can be used in java program only if it has first been declared.
▪ Syntax: <type-name> {variable-names}; Examples: int marks;
double amount, interest;
Rules
– Must begin with an alphabet, underscore or dollar sigh.
– Must begin with a character.
– No spaces are allowed in variables.
– It cannot be a reserved word.
Variable
Guidelines:
– Choose meaning name.
– Java allows variable name of any length.
– Capitalize the fist letter of each word, except the
first word. This is referred to as camel case.
Example: balanceAmount, birthDate
– Separate words with underscore.
– Java is case sensitive.
– It is customary for names of classes to begin with
uppercase letters, while names of variables and of
methods begin with lower case letters.
Literals
▪ Class Representation in UML convention Diagram of class
‘Person’
Literals
▪ A name used for a constant value is known as literal.
▪ A Unicode literal consist of \u followed by four hexadecimal
digits.
▪ Real number literals are floating point literals.
▪ They can be represented using two notations:
– Standard and Scientific e.g. 12.37 and 1.3e12
▪ By default floating point literal is of type double. To make
literal of type float, append “F” or “f” as suffix. Eg. 1.2F
Literals
Expression
A combination of
▪ operators,
▪ constants and
▪ variables.
▪ Consist of one or more
operands, and
▪ zero or more operators
▪ to produce a value.
Operators
Operators
Operators and Associativity
Control Structure
If, if-else, if-elseif
Switch case
Iterative – for, while an do..while