Basic overview of Java, Datatypes, Variables and JVM

Introduction of Java

Basic overview of Java, Datatypes, Variables and JVM

Introduction

You can ask a question like, why we need any programming language?

Check This -> Need of Language for understanding

ava.jpg

JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. It was developed by James Gosling and Patrick Naughton. It is a simple programming language. Writing, compiling and debugging a program is easy in java. It helps to create modular programs and reusable code.

How Java Works?

java work.png

Features of Java

Apart from being a system-independent language, there are other reasons too for the immense popularity of this language. Let us have a look at some of its features

  1. Simple Java is a simple programming language. Rather than saying this is the feature of Java, we can say this is the design aim of Java. When Java is developed they wanted it to be simple because it has to work on electronic devices where less memory is available. Now the question is how Java is made simple? All the difficult concepts of C, C++ have been omitted in java. For example, the concept of pointers which is very difficult for both learners and programmers has been eliminated in java.

  2. Object-Oriented Java is an object-oriented programming language. This means java programs use objects and classes.

  3. Robust Robust means are strong. Java programs are strong and they don't crash easily. There are two reasons for this. Firstly, Java has got excellent inbuilt exception handling. Another reason lies in its memory management features. Most of the C, C++ programs crash in the middle because of not allocating sufficient memory or forgetting the memory to be freed in a program. Such problems will not occur in Java because the user need not allocate or deallocate the memory in java. Everything will be taken care by the JVM only. For example, JVM will allocate the necessary memory required by a Java program.

  4. Secure Security problems like tampering, impersonation, and virus threats can be eliminated or minimized by using java on the internet.

  5. System independent Java's byte code is not machine-dependent. It can be run on any machine with any processor and any operating system.

  6. Portability: If a program yields the same result on every machine, then that program is called portable Java programs are portable this is because of its System independent nature.

  7. Interpreted: Java programs are compiled to generate byte code. This byte code can be downloaded and interpreted by the interpreter in JVM. If we take any other language only a compiler or interpreter is used to execute a program. But in java, we use both compiler and interpreter for execution.

  8. MultiThreaded A lot of action can be performed simultaneously.

# What is Java Virtual Machine (JVM) ?

jvm.jpg

Java Virtual machine is the heart of the entire Java program execution process. It is responsible for taking the .class file and converting each byte code instruction into machine language instruction that can be executed by the microprocessor.

First of all, the .java program is converted into a .class file consisting of byte code instructions by the java compiler. Remember, this java compiler is outside the JVM. Now, this .class file is given to the JVM. In JVM, there is a module called class loader subsystem , which performs the following functions:

First of all, it loads the .class files into memory. Then it verifies whether all byte code instructions are proper or not. If it finds any instruction suspicious, the execution is rejected immediately. If the byte instructions are proper, then it allocates the necessary memory to execute the program.

jvm.png

This memory is divided into 5 parts:

  1. Method area: The method area is the memory block that stores the class code, code of the variables, and the code of methods in the java program.

  2. Heap: This is the area where objects are created. Whenever JVM loads a class, a method and heap area is immediately created in it.

  3. Java Stacks: Method code is stored in the method area. But while running a method, it needs some more memory to store the data and results. This memory is allocated on the java stack. So java stacks are memory areas where java methods are executed.

  4. Program Counter (PC) : These are registers, which contain the memory address of the instructions of the methods. If there are 3 methods, 3 PC registers will be used to track the instructions of the methods.

  5. Native method stack: Java methods are executed on java stacks. Similarly, native methods are executed on Native method stacks. To execute the native methods, generally, native method libraries(for example C/C++ header files) are required. These header files are located and connected to JVM by a program called Java Native Interface (JNI).

Java Syntax:

public class MyClass {
  public static void main(String[] args) {
  System.out.println("Hello World");
  }
}

Every line of code that runs in Java must be inside a class. In our example, we named the class MyClass. A class should always start with an uppercase first letter.

Note:

Java is case-sensitive: "MyClass" and "myclass" has different meaning. The name of the java file must match the class name. When saving the file, save it using the class name and add ".java" to the end of the filename The main Method:

The main() method is required and you will see it in every Java program:

public static void main(String[] args);

main() is the point where the program starts executing. System.out.println():

We can use the println() method to print a line of text to the screen: Note: 1.The curly braces {} marks the beginning and the end of a block of code. 2.Each code statement must end with a semicolon.

Java Comments:

Comments can be used to explain Java code, and to make it more readable. It can also be used to prevent execution when testing alternative code.

Single-line comments start with two forward slashes (//).

Any text between // and the end of the line is ignored by Java (will not be executed).

This example uses a single-line comment before a line of code: Example:

// This is a comment

System.out.println("Hello World");

Output

Hello World

Java Multi-line Comments:

Multi-line comments start with / and ends with /.

Any text between / and / will be ignored by Java.

This example uses a multi-line comment (a comment block) to explain the code: Example:

/ The code below will print the words Hello World to the screen, and it is amazing /

System.out.println("Hello World");

Output

Hello World

For more information refer Datatypes

Data types are divided into two groups:

Primitive data types - includes byte, short, int, long, float, double, boolean and char Non-primitive data types - such as String, Arrays and Classes. Integer Types

byte

The byte data type can store whole numbers from -128 to 127. This can be used instead of int or other integer types to save memory when you are certain that the value will be within -128 and 127:

Example

byte myNum = 100;
System.out.println(myNum);

Output 100

short

The short data type can store whole numbers from -32768 to 32767:

Example

short myNum = 5000;
System.out.println(myNum)

Output 5000

int

The int data type can store whole numbers from -2147483648 to 2147483647. In general, and in our tutorial, the int data type is the preferred data type when we create variables with a numeric value.

Example

int myNum = 100000;
System.out.println(myNum);

Output

100000

long

The long data type can store whole numbers from -9223372036854775808 to 9223372036854775807. This is used when int is not large enough to store the value. Note that you should end the value with an "L":

Example

long myNum = 15000000000L;
System.out.println(myNum);

Output

15000000000

Floating Point Types

You should use a floating point type whenever you need a number with a decimal, such as 9.99 or 3.14515. float

The float data type can store fractional numbers from 3.4e−038 to 3.4e+038. Note that you should end the value with an "f": Float gives a accuracy of upto 6 or 7 digits after decimal points.

Example 1

float myNum = 5.75f;
System.out.println(myNum);

Output

5.75

Example 2

float value = 50.786868685678f;
System.out.println(value);

Output

50.7868686

double

The double data type can store fractional numbers from 1.7e−308 to 1.7e+308. The double gives an accuracy upto 15 digits after decimal points.

Example

double myNum = 19.999191191919;
System.out.println(myNum);

Output

19.999191191919

boolean

A boolean data type is declared with the boolean keyword and can only take the values true or false:

Example

boolean isJavaFun = true;
boolean isFishTasty = false;
System.out.println(isJavaFun);     // Outputs true
System.out.println(isFishTasty);   // Outputs false

char

The char data type is used to store a single character. The character must be surrounded by single quotes, like 'A' or 'c':

Example

char myGrade = 'B';
System.out.println(myGrade);

Java Variables

Syntax:

type variable = value;

Variables are containers for storing data values.

In Java, there are different types of variables, for example:

String - stores text, such as "Hello". String values are surrounded by double quotes
int - stores integers (whole numbers), without decimals, such as 123 or -123
float - stores floating point numbers, with decimals, such as 19.99 or -19.99
char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
boolean - stores values with two states: true or false

Example

Create a variable called value of type String and assign it the value "Masai"

String value = "Masai";
System.out.println(value);

Output

Masai

To create a variable that should store a number, look at the following example:

Example

Create a variable called myNum of type int and assign it the value 15:

int myNum = 15;
System.out.println(myNum);

Output

15

You can also declare a variable without assigning the value, and assign the value later:

Example

int myNum;
myNum = 15;
System.out.println(myNum);

Output

15

Note that if you assign a new value to an existing variable, it will overwrite the previous value:

Example

Change the value of myNum from 15 to 20:

int myNum = 15;
myNum = 20;  // myNum is now 20
System.out.println(myNum);

Output

20

Other Types

A demonstration of how to declare variables of other types:

Example

int myNum = 5;
float myFloatNum = 5.99f;
char myLetter = 'D';
boolean myBool = true;
String myText = "Hello";

Key Points to remember-

  • Java is case sensitive and independent language.

  • Java have compiler as well as interpreter.

  • Use the data type wisely, to save the memory space.

  • Use of JVM

Quote of Day -

Reason to quit are always available but so are reason to continue and finish.

Keep Learning!