Introduction To Java
Introduction To Java
Java is one of most popular high level ‘Object Oriented’ programming language and has been used
widely to create various computer applications.
Applications of java
Database applications
Desktop applications
Mobile applications
Games etc.
What is JVM?
JVM stands for Java Virtual Machine which is basically a java interpreter which translates the
byte code into machine code and then executes it.
The advantage of JVM is that once a java program is compiled into byte code. It can be run on
any platform.
Byte code is an highly optimized intermediate code produced when a java program is compiled
by java compiler.
Byte code is also called java class file which is run by java interpreter called JVM.
Compiler
Debugger
What is NetBeans?
Java NetBeans IDE is open source IDE used for writing java code, compiling and executing java programs
conveniently.
This is especially useful where large programs are written by one programmer and maintained
by other programmers.
You can write comments in a Java program in the following two ways:
Variables
Variable is a placeholder for data that can change its value during program execution.
Technically, a variable is the name for a storage location in the computer’s internal memory and
the value of the variable is the contents at that location.
Data types
Data type tells the compiler, how much memory to reserve when storing a variable of that type.
Declaring variable
int x;
float degree;
int a,b,c;
char ac_no;
Variable names can begin with either an alphabetic character, an underscore (_), or a dollar sign
($).
Variable names must be one word. Spaces are not allowed in variable names. Underscores are
allowed.
There are some reserved words in Java that cannot be used as variable names, for example – int
Java is a case-sensitive language. Variable names written in capital letters differ from variable
names with the same spelling but written in small letters.
To take user input we use the prebuilt Scanner class. This class is available in the java.util package.
Then we create object of Scanner class (we pass System.in as argument in its constructor
because we will take input from console) as:
Scanner user_input = new Scanner(System.in)
Then we invoke next() method of Scanner class that returns input taken from input stream as
String object as:
String name = user_input.next()
Note: to read numeric data input, we use static method parseInt() of Integer class that takes String as
parameter and returns its equivalent as given below:
String s = user_input,next();
int num = Integer.parseInt(s);
Operators
Operators are special symbols in programming language and perform certain specific operations.
Control flow
A program is considered as finite set of instructions that are executed in a sequence. But sometimes the
program may require executing some instructions conditionally or repeatedly. In such situations java
provides:
Selection structures
Repetition structures
Selection structure
If else statement
Switch statement
if else statement
The if statement in Java lets us execute a block of code depending upon whether an expression
evaluates to true or false. The structure of the Java if statement is as below:
Sometimes, you may want to execute a block of code based on the evaluation of two or more
conditional expressions. For this, you can combine expressions using the logical && or || operators.
Multiple if statements
It is basically used to execute more than two block of statements on the evaluation of different
conditions.
The switch statement is used to execute a block of code matching one value out of many
possible values.
It is basically used to implement choices from which user can select anyone.
Repetition in program refers to performing operations (Set of steps) repeatedly for a given
number of times (till the given condition is true).
while
do..while
for
while statement
Syntax:
do..while statement
It executes the statements at least once even when given condition is not true.
Syntax:
Java program to display multiples of 5 between 50 and 100
for statement
Syntax:
Example:
Array
Arrays are variables that can hold more than one value of the same type, name and size.
It occupies contiguous memory space when declared and each space is called Array Elements
which can store individual value.
Each Array Element is uniquely identified by a number called Array Index which begins from 0.
The [] brackets after the data type tells the Java compiler that variable is an array that can hold
more than one value.
Syntax:
Example:
A method has a name, a return type, an optional list of parameters, and a body.
Parameters placed within brackets are like normal variables that allow data to be passed into
the method.
In OOP language program is collection of objects that interact with other object to solve a
problem.
To use attributes and invoke method of class must be associated with its corresponding object.
From a class multiple objects can be declared and all objects can have the same type of data
members.
Designing a class
Syntax:
Example
Constructor
Constructor is a special method which is used to initialize the data members of the class.
The constructor has no return type and may or may not have parameter list.
Example:
Access Modifier
Access Modifiers in java are keywords that specify the accessibility scope of data members of
class.
private
public
Member methods are generally kept as public to make them available to access.
String manipulation