1. History of Java Language


The Java language project was started by James Gosling, Mike Sheridan, Chris Warth, Patrick Naughton and Ed Frank in June 1991 at Sun Microsystems. Many more people contributed for this project. They are Bill Joy, Jonathan Payne, Frank Yellin, Arthur van Hoff and Tim Lindholm.

The initial aim of the project was to create a programming tool that can be used to control an electronic device remotely.

History of Java Language (Logo)

The language was initially called Oak after an oak tree that stood outside Gosling's office; later on it was named as Green. Finally the language renamed as Java because during this project the team had consumed a lot of coffee which was imported from island named as Java. This is also the reason why Java has its logo as a cup of coffee.

Sun Microsystems released the first version of Java publicly as Java 1.0 in 1995. As time passed many new versions were released. The latest Java version is 1.7 which is also called as Java 7.

Java has many interesting features like "write once, run anywhere" (WORA), it is platform independent, secure and robust. These features will be discussed in detail in the next article.

James Gosling

There were five primary goals for creating the Java language

1. It should be "simple, object-oriented and familiar"
2. It should be "robust and secure"
3. It should be "architecture-neutral and portable"
4. It should execute with "high performance"
5. It should be "interpreted, threaded, and dynamic"


Simple


  • Java derives its syntax from C and object oriented features from C++.
  • If you are good in C and C++ programming then you can learn Java easily. Hence Java is Simple.



Secure


  • Java provides its own execution environment and hence do not allow any malicious program to access other parts of computer.
  • Java Virtual Machine (JVM) verifies the code before execution (sandbox security).

C Security

Java Security



 Portable


  • Java is platform independent i.e. Write Once and Run Anywhere (WORA).
  • When java program is compiled a .class file (bytecode) is created. This .class file can run on any platform (linux, windows, mac). You just need JVM of that platform.

Platform Independent


Robust

Java is called as robust because of following two features:
  • Automatic Memory Management
  • Automatic Exception Handling


Object-Oriented


  • Java is object oriented programming language same as C++.
  • It means focus on the data and methods that manipulate that data instead of taking care of procedures.


Multithreaded


  • Java supports multithreaded programming.
  • It allows you to write programs that do many things simultaneously.



Architecture-Neutral


  • Java is machine independent.
  • It was made with goal “write once; run anywhere, anytime, forever”.


Interpreted


  • Java enables the creation of cross-platform program i.e. bytecode.
  • This code can be executed on any platform that has JVM.



High Performance


  • Java bytecode can be easily executed on any machine for very high performance by using a just-in-time (JIT) compiler.



Dynamic


  • Java programs carry with them substantial amounts of run-time type information that is used to verify and resolve accesses to objects at run time.


Distributed


  • Java enables the creation of distributed applications which can be accessed on the internet.
        Image Source: http://www.javatpoint.com/features-of-java


In this tutorial we will learn how to download and install Java and then setting path to make Java programs run.

Downloading and Installing


JDK Download



  • Select Accept License Agreement and then download appropriate version for your operating system.
JDK Versions



  • After downloading, install the JDK.
  • If installation is done properly you can see two files java and javac by going to C:\Program Files\Java\jdk1.7.0_60\bin.



How to set Java path?

To make java programs compile and run you have to set the path of java compiler. You can set java path in two ways: temporary and permanent.

Temporary Java path setting

  • Copy the path of bin folder by going to jdk/bin i.e. C:\Program Files\Java\jdk1.7.0_60\bin. Here jdk1.7.0_60 will vary according to jdk version.

bin path


  • Open command prompt and write set path=C:\Program Files\Java\jdk1.7.0_60\binand then press enter.

set temporary path


  • This method will work only one time. After closing cmd you have to repeat the same process again.



Permanent Java path setting

  • Right click on Computer and then select Properties.
  • Then goto Advanced tab and click on Environment Variables.
  • Now click on new and then in variable name field write pathand in value field write the path of bin folder i.e. C:\Program Files\Java\jdk1.7.0_60\bin

set permanent path

  • Click ok to save settings. You have done!
  • This will permanently set the path and you can run programs whenever you want.



Check path setting was properly or not

  • You can confirm whether the path setting was properly or not. Just open cmd again, write javacand press enter.

Check path setting was properly or not

  • If it will show details about the java compiler then it is ok.
  • If it will show and error like ‘javac’ is not recognized as an internal or external commandthen you have done something wrong. Check the whole process again.

Before running Java programs you need to install and setup java.

How to write and run Java programs?

  • Open notepad and write below program.


class demo
{
                public static void main(String...S)
                {
                                System.out.println("Java Programming");
                }
}

First Java Program





  • Now save the file with name demo.java on desktop. Here you can replace demo with any other name that you want.
  • Then open command prompt (cmd) and type cd desktop and press enter to change current directory to desktop. We changed directory because our program is saved on desktop.

cd desktop

  • To compile java program type javac demo.java and hit enter. Type java demo to run the program. Remember that here we have to compile with program name and execute with class name that we have used in program. The java program name and class name can be different.

compile and run


Understand the Program


Understand the Program


System: Get reference id of the device on which program is running. Here the device is cmd.
out: Store the reference id of device.
println: Throw the output on device.

A.java
A.java
A.java
class A
 {

}
class A
{
public static void main()
{
}
}
compile
Yes
Yes
Yes
.class file
Not created
Created
Created
execute
No
No
Yes

This tutorial will help you to learn about what is .class file and bytecode in java language. You may like to read previous tutorial about How to Write, Compile and Run Your First Java Program?


What is .class File and Bytecode

  • When Java source file is compiled by Java compiler it is converted into Java class file with .class extension.
  • The Java class file contains Java byte code (highly optimized set of instructions) which is executed by Java Virtual Machine (JVM).
  • .class file contains symbols and each bytecode instruction is stored into one byte exactly.
  • .class file of each class is separately stored. Its name is same as class name in source file. A Java program can have any number of classes. If below program is compiled then two .class file will be created with name A.class and B.class.

class A
{

public static void main(String...s)

{

System.out.println(“A”);

}

}


class B
{

public static void main(String...s)

{

System.out.println(“B”);

}

}

  • A .class file can be executed on any operating system; we just need JVM of that operating system. This makes Java machine independent.

Difference Between .class File in Java and .exe File in C/C++

.class
.exe
It contain bytecode instructions for Java Virtual Machine (JVM).
It contains machine code instructions for Operating System.
Platform independent.
Platform dependent.
It can be converted into source code.
It can’t be converted into source code.
The .class file can’t be executed directly so viruses can’t be attached to it and hence secure.
The .exe file can be executed directly so viruses can be attached to it and hence insecure.
  • JVM is responsible for taking .class file and converting that .class file in machine code instructions that can be executed by microprocessor.
  • It’s not a machine, it’s a program.
  • JVM verifies the code before execution (Sandbox Security).
  • JVM is platform dependent and it is heart of Java language.


java program execution

There are four phases of JVM

  • Load Code
  • Verify Code
  • Execute Code
  • Provide Runtime Environment

Overview of Java virtual machine (JVM) architecture and structure


Java virtual machine architecture


Java Virtual Machine Structure



Variable is nothing it is just the name of memory location. While declaring variables we must follow rules given below.

Java Variable Types and Rules for Declaring Variables

Rules for Declaring Variables in Java

1. Variable name must bound with data type. It means while declaring a variable we must specify its data type.
Ex: int x=10;

2. Reserve word or keywords cannot be taken as a variable name.

3. C supports 32 character long variable names while in Java the length of variable name can be infinite.

4. Nothing except 0-9, A-Z, a-z, _, $ can be used in variable name.

5. Variable name must not start with numeric and special characters are also not allowed.
Example:
intabc=7; (valid)
int _abc=8; (valid)
int abc8=9; (valid)
int 8abc=10 (invalid)
int $_abc=12; (valid)
int $_abc*=15; (invalid)

6. Variable name should be meaningful.
Example:
string i=”Raj”;  //valid but not meaningful
string name=”Raj”; //valid and meaningful

Note: Local variables must be initialized before first use.

class demo
{
                public static void main(String…s)
                {
                                int x;      //must be initialized
                                System.out.println(x);
                }
}

Java Variable Types

There are three types of variables in Java that are instance, local and static.

1. Instance Variables

Instance variables are declared in a class, but outside any method, constructor or block.

2. Local Variables

Local variables are declared in methods, constructors or blocks. It must be initialized before first use.

3. Static Variables

Static variables are declared with the static keyword in a class, but outside any method, constructor or block.

class A
{
                int x=12;               //instance variable
                staticint y=13;    //static variable
               
                public static void main(String…s)
                {
                                int z=30;               //local variable
                                System.out.println(x);
                                System.out.println(y);
                                System.out.println(z);
                }

}

Data Types in Java

  • Used to represent the type of memory allocation.
  • Used with variables and methods.
  • These are fixed.

Data Types in Java

Primitive Data Types

Primitive data types are defined already by the programming language. In Java there are 8 primitive data types.

Primitive Data Types

Non-Primitive Data Types

Non-Primitive data types are defined by the user. They are Class, Array, Interface and String.

Naming Conventions in Java

There are few naming conventions defined by the Java language that should be kept in mind while giving name of a class, interface, function, etc.

  • Class and Interface: First letter of each word should be uppercase. Ex: MyClassName.
  • Method: First letter of first word is lowercase while first letter of other words is uppercase. Ex: myMethodName().
  • Package and Keyword: In lowercase. Ex: int, float, static, etc.


Converting one data type to another data type is known as type casting. Type Casting in Java is of two types i.e. Implicit Type Casting which is also known as Automatic Type Casting and Explicit Type Casting.

Implicit or Automatic Type Casting

  • In this, smaller data type is converted to bigger data type which is also known as widening.
  • There is no data loss chance.
  • It is done by compiler therefore it is known as automatic type casting.
  • Example: byte b=10; int x=b; Here byte type is converted to int type.

Explicit Type Casting

  • In this, bigger data type is converted to smaller data type which is also known as narrowing.
  • There is data loss chance.
  • It is done by the user with the help of type cast operator i.e. ( ).
  • Example: int x=10; byte b=(byte)x; Here int type is converted to byte type.

Type Promotion in Java

  • Converting smaller data type to bigger data type is known as type promotion.
  • Only promotion is allowed, demotion is not allowed.

Type Promotion Chart

  • This chart tells us about the flow of converting one smaller data type to another bigger data type.
  • Double can’t be converted to any data type.
Type Promotion Chart and Type Casting in Java

Note: Long takes 8 bytes while float takes 4 bytes, instead of that long (bigger type) can be converted to float (smaller type) which is an exception.
If you find anything missing or wrong in above tutorial then please mention it by commenting below.


0 comments :

Please Enter best of your Comments