Java Basics 1 - Chapter One
Java Basics 1 - Chapter One
Chapter 1
Java Platform is a collection of programs that help programmers to develop and run Java
programming applications efficiently. It includes an execution engine, a compiler, and a set
of libraries in it. It is a set of computer software and specifications. James Gosling
developed the Java platform at Sun Microsystems, and the Oracle Corporation later
acquired it.
Sun released the first public implementation as Java 1.0 in 1995. It promised Write Once, Run
Anywhere (WORA), providing no-cost run-times on popular platforms.
On 13 November 2006, Sun released much of Java as free and open-source software under the terms
of the GNU General Public License (GPL).
On 8 May 2007, Sun finished the process, making all of Java's core code free and open-source, aside
from a small portion of code to which Sun did not hold the copyright.
• Simple
Java is very easy to learn, and its syntax is simple, clean and easy to understand. According to Sun
Microsystem, Java language is a simple programming language because:
• Java syntax is based on C++ (so easier for programmers to learn it after C++).
• Java has removed many complicated and rarely-used features, for example, explicit pointers,
operator overloading, etc.
• There is no need to remove unreferenced objects because there is an Automatic Garbage
Collection in Java.
• Object Oriented
Java is platform independent because it is different from other languages like C, C++, etc. which are
compiled into platform specific machines while Java is a write once, run anywhere language. A
platform is the hardware or software environment in which a program runs.
There are two types of platforms software-based and hardware-based. Java provides a software-
based platform.
The Java platform differs from most other platforms in the sense that it is a software-based platform
that runs on top of other hardware-based platforms. It has two components:
1.Runtime Environment
2.API(Application Programming Interface)
• Portable
Java is portable because it facilitates you to carry the Java bytecode to any platform. It
doesn't require any implementation.
Java is faster than other traditional interpreted programming languages because Java bytecode is "close"
to native code. It is still a little bit slower than a compiled language (e.g., C++). Java is an interpreted
language that is why it is slower than compiled languages, e.g., C, C++
• Multi Threaded
A thread is like a separate program, executing concurrently. We can write Java programs that deal with
many tasks at once by defining multiple threads. The main advantage of multi-threading is that it doesn't
occupy memory for each thread. It shares a common memory area. Threads are important for multi-media,
Web applications, etc.
• Architecture Neutral
Java is architecture neutral because there are no implementation dependent features, for example, the size
of primitive types is fixed.
In C programming, int data type occupies 2 bytes of memory for 32-bit architecture and 4 bytes of memory
for 64-bit architecture. However, it occupies 4 bytes of memory for both 32 and 64-bit architectures in Java.
• Secured
Java is best known for its security. With Java, we can develop virus-free systems. Java is secured
because:
JDK is an implementation of any one of the below given Java Platforms released by Oracle corporation:
The JDK contains a private Java Virtual Machine (JVM) and a few other resources such as an
interpreter/loader (Java), a compiler (javac), an archiver (jar), a documentation generator (Javadoc) etc.
to complete the development of a Java Application.
Java source code is compiled into bytecode when we use the javac compiler. The bytecode gets saved on
the disk with the file extension .class. When the program is to be run, the bytecode is converted, using the
just-in-time (JIT) compiler. The result is machine code which is then fed to the memory and is executed.
The Java classes/bytecode are compiled to machine code and loaded into memory by the JVM when
needed the first time. This is different from other languages like C/C++ where programs are to be compiled
to machine code and linked to create an executable file before it can be executed.
Reserved Areas
10 A
Types of Variables
“Java”
There are three types of variables in Java:
1. local variable
RAM (Random Access 2. instance variable
Memory)
3. static 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.
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.
void method()
{
int n=90;//local variable
}
public static void main(String args[])
{
int data=50;//Local variable
}
}//end of class
Primitive data types: The primitive data types include boolean, char, byte, short,
int, long, float and double.
Non-primitive data types: The non-primitive data types include Classes, Interfaces,
and Arrays.
Widening
Narrowing is a process in which a larger type is converted to smaller type. This needs to be done
explicitly by the programmer. This is also known as Downcasting. Explicit conversion of a
superclass reference variable to a subclass reference variable is also part of Narrowing.
Operator in Java is a symbol that is used to perform operations. For example: +, -, *, / etc.
There are many types of operators in Java which are given below:
1. Unary Operator
2. Arithmetic Operator
3. Shift Operator
4. Relational Operator
5. Bitwise Operator
6. Logical Operator
7. Ternary Operator
8. Assignment Operator
The Java unary operators require only one operand. Unary operators are used to perform various
operations i.e.:
Java arithmetic operators are used to perform addition, subtraction, multiplication, and division.
They act as basic mathematical operations.
The Java left shift operator << is used to shift all of the bits in a value to the left side of a specified
number of times.
The Java right shift operator >> is used to move the value of the left operand to right by the
number of bits specified by the right operand.
Java Ternary operator is used as one line replacement for if-then-else statement and used a lot in
Java programming. It is the only conditional operator which takes three operands.
Java assignment operator is one of the most common operators. It is used to assign the value on
its right to the operand on its left.