Java Part1
Java Part1
1|Page
PROGRAMMING IN JAVA – II BCA [GTMC]
HISTORY OF JAVA
• The C language developed in 1972 by Dennis Ritchie had taken a decade to become
the most popular language.
• In 1979, Bjarne Stroustrup developed C++, an enhancement to the C language with
included OOP fundamentals and features.
• A project named “Green” was initiated in December of 1990, whose aim was to
create a programming tool that could render obsolete the C and C++ programming
languages.
• Finally in the year of 1991 the Green Team was created a new Programming
language named “OAK”.
• After some time they found that there is already a programming language with the
name “OAK”.
• So, the green team had a meeting to choose a new name. After so many
discussions they want to have a coffee. They went to a Coffee Shop which is just
outside of the Gosling’s office and there they have decided name as “JAVA”.
2|Page
PROGRAMMING IN JAVA – II BCA [GTMC]
FEATURES OF JAVA
3.Object-Oriented:
Java is Object oriented throughout language- that mean no coding outside of
class definitions, including main().
4. Robust:
Robust means inbuilt capabilities to handle errors/exceptions.
Java is robust because of following:
1. Built-in Exception handling.
2. Strong type checking i.e. all data must be declared an explicit type.
3. Local variables must be initialized.
4. First checks the reliability of the code before Execution etc.
3|Page
PROGRAMMING IN JAVA – II BCA [GTMC]
5. Secure:
Java is secure because it provides:
1. access restrictions with the help of access modifiers (public, private etc).
2. byte codes verification – checks classes after loading.
Class loader – confines objects to unique namespaces.
Security manager – determines what resources a class can access such as reading and
writing to the local disk.
6. Compiled and interpreted:
Java code is translated into byte code after compilation and the byte code is interpreted
by JVM (Java Virtual Machine). This two steps process allows for extensive code checking
and also increase security.
7. Portable:
Means able to be easily carried or moved. Write once, run anywhere (WORA) feature
makes it portable.
8. Architecture-Neutral:
Java code is translated into byte code after compilation which is independent of any
computer architecture, it needs only JVM (Java Virtual Machine) to execute.
9. High performance:
JVM can execute byte codes (highly optimized) very fast with the help of Just in time (JIT)
compilation technique.
10. Re-usability of code:
Java provides the code reusability With the Help of Inheritance.
11. Multithreading:
Java provides multitasking facility with the help of lightweight processes called threads.
12. Dynamic:
Java have the capability of linking dynamic new classes, methods and objects.
4|Page
PROGRAMMING IN JAVA – II BCA [GTMC]
Java Identifiers
Keywords in Java
Keywords in Java are reserved words that represent predefined actions, internal
processes etc. Because of this, keywords cannot be used as names of variables, functions,
objects etc.
Some of the keywords in the Java are given as follows −
volatile while
5|Page
PROGRAMMING IN JAVA – II BCA [GTMC]
Procedure to develop Java program
class FirstProgram
{
public static void main(String args[])
{
System.out.println(“Welcome Java”);
}
}
Save: FirstProgram.java
Compile: C:\> javac FirstProgram.java
Run: C:\> java FirstProgram
6|Page
PROGRAMMING IN JAVA – II BCA [GTMC]
Explanation:
• class is a keyword in Java and everything is written under the class.
• public is an access modifier which provides the accessibility of main method to all.
• static is a keyword and main methods are always declared as static. There is no
need to create an object to invoke the static method.
• void shows the main methods that do not return any kind of value.
• String args[] is used for command line argument.
• System.out.println() is used for printing the statement.
The following three steps are used to create and execute a java program.
7|Page
PROGRAMMING IN JAVA – II BCA [GTMC]
Variable in Java
• Variable is an identifier which holds data or another one variable is an identifier
whose value can be changed at the execution time of program.
• Variable is an identifier which can be used to identify input data in a program.
Syntax
Variable_name = value;
8|Page
PROGRAMMING IN JAVA – II BCA [GTMC]
Variable declarations
Syntax
Datatype variable_name;
byte b1;
Variable initialization
It is the process of storing user defined values at the time of allocation of memory space.
Variable assignment
Syntax
Variable_Name = value
int a = 100;
9|Page
PROGRAMMING IN JAVA – II BCA [GTMC]
Syntax
int a= 100;
int b;
b = 25; // ------> direct assigned variable
b = a; // ------> assigned value in term of variable
b = a+15; // ------> assigned value as term of expression
o local variable
o instance variable
o static variable
1) Local Variable
A variable declared inside the body of the method is called local variable. You can use this
variable only within that method and the other methods in the class aren't even aware
that the variable exists.
2) Instance Variable
A variable declared inside the class but outside the body of the method, is called an
instance variable. It is not declared as static.
It is called an instance variable because its value is instance-specific and is not shared
among instances.
10 | P a g e
PROGRAMMING IN JAVA – II BCA [GTMC]
3) Static variable
A variable that is declared as static is called a static variable. It cannot be local. You can
create a single copy of the static variable and share it among all the instances of the class.
Memory allocation for static variables happens only once when the class is loaded in the
memory.
11 | P a g e
PROGRAMMING IN JAVA – II BCA [GTMC]
Java Comments
Comments in Java allow you to describe the code. This makes the Java code easy to read.
Whatever you add inside a comment never gets compiled. Comments in a Java program
can be mentioned in the following two ways:
• Single-line Comment
• Multi-line Comments
Single-line comments
Use // i.e., two forward slashes for a single-line comment in Java. Whatever we mention
after the slashes in a single line, will not get compiled.
Multi-line comments
For multi-line comments in Java, use /* */. Whatever we mention under /* and */, will
not get compiled.
12 | P a g e
PROGRAMMING IN JAVA – II BCA [GTMC]
Java Type Casting
The Primitive datatypes in Java are the following: byte, short, int, long, float, double,
boolean, and char. Type Casting is to convert one data type to another. There are two
types of Type Casting in Java:
13 | P a g e
PROGRAMMING IN JAVA – II BCA [GTMC]
Java Data Types
Java programming language has a rich set of data types. The data type is a category of
data stored in variables. In java, data types are classified into two types and they are as
follows.
• The primitive data types are built-in data types and they specify the type of value
stored in a variable and the memory size.
• The primitive data types do not have any additional methods.
14 | P a g e
PROGRAMMING IN JAVA – II BCA [GTMC]
The following table provides more description of each primitive data type.
15 | P a g e
PROGRAMMING IN JAVA – II BCA [GTMC]
Non-primitive Data Types
• In java, non-primitive data types are the reference data types or user-created data
types.
• The non-primitive data types may use additional methods to perform certain
operations.
16 | P a g e
PROGRAMMING IN JAVA – II BCA [GTMC]