Friday, January 15, 2021

Data Types in Java

 Data Types in Java

Data Type in Java is defined as that specifies the size of memory allocated for a specific variable and which type of value is stored in an identifier. In Java there is mainly two types of data type:-
1. Primitive Data Type
2. Non-Primitive Data Type
for example, int a; //where int is the data type and a is variable

Primitive Data Type

Java defined eight different types of primitive data types like int, short, long, byte, float, double, char, and boolean. Integers group contain four data type int, short, long, byte. The floating-point numbers group contains float and double data type numbers. Char and boolean both are different.

int Data Type

The most commonly used integer type is int. It is a signed 32-bit type that has a range from -2,147,483,648 to 2,147,483,647.
Syntax: <datatype>name of variable  
for example- int b;

short Data Type

short is a signed 16-bit type. It has a range from -32,768 to 32,767. If our value in the program not more than this range then we recommended this type of data type.
Syntax: <datatype>name of variable 
for example: short c;

byte Data Type

This is the smallest integer type of data type. It's range from-128 to 127 and 8-bit requires.
Syntax: <datatype>name of variable 
for example byte d;

long Data Type

long data type is used when int is not large enough to hold the value. It's range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. It's require 64-bit in memory.
Syntax: <datatype>name of variable 
for example long e;

Floating-Point Types

Floating -point numbers, are also known as real numbers, are used when evaluating the expression that requires fractional precision. For example, calculations such as the square root of any numbers and other mathematics expressions. It has mainly two types:
1. float
2. double

float Data type

It requires a 32-bit size and sufficient for holding 6 to 7 precision value.
for example: float a=2.5564f
where F defines the float variable

double Data Type

It requires a 64-bit size and sufficient for holding 15 precision value.
double h=2.45247d
where d defines the double variable

boolean Data Type

It stores logical values. It can have only one of two possible values, true or false.
boolean b;

char Data Type

char data type store or hold a character. It requires 16-bit.
for example char ch='M';

Program for data types in java.

public class DataType{
public static void main(String[]args){
int a=2147483647; 
byte b=127;
short c=32767;
long d=12332252626L;
float e=2.1234567F;
double g=2.12345678998765434d;
boolean i=true;
char ch='M';

System.out.println("Integer number is="+a);
System.out.println("byte number is="+b);
System.out.println("short number is="+c);
System.out.println("long number is="+d);
System.out.println("float number is="+e);
System.out.println("double number is="+g);
System.out.println("boolean number is="+i);
System.out.println("char number is="+ch);

}
}

Non-Primitive Data Type

Non-Primitive Data Type is called reference type because they refer to the objects. For example arrays and strings.

Arrays

An array is a group of like type variables that are referred to as a common name. The syntax for declaring an array in java program: data type[] arrayName; or data type arrayName[];
for example double myName[]; or double []myName;

Strings

The String data type is used to store a sequence of characters. Strings must be surrounded by double-quotes.
for example string m="codehubpoint";


Wednesday, January 13, 2021

Variables in Java

Variables in Java

A variable is a name given to a memory location. It is the unit of storage in the program. The value stored in a variable can be changed during program execution.
For example- int a;   // where int is data type and a is variable

Java variables type:

Local variable
Instance variable
Static variable

Local variable:

A local variable is declared inside the body of the method is called the local variable. A Local variable cannot be defined with a static keyword. No object requires to access the Local variable. The scope of these variables exists only within the method in which variable the variable is declared.

Instance variable:

An instance variable is declared inside the class but outside the body of the method is called the Instance variable. It is not declared with the static keyword. These variables are accessed through the object of the class. 

Static variable:

A static variable is declared inside the class but outside the body of the method with the static keyword is called Static variable. It is similar to the instance variable but the difference is the static variable declared with the static keyword while the instance variable declared without the static keyword.

Sample program:

public class Variables{
static int a=10; // static variable
int b=20;  // Instance variable
public static void main(String args[]){
int c=30;  //Local variable
System.out.println("static variable is:"+a);
System.out.println("local variable is:"+c);
Variables obj=new Variables(); //object created for instance variable
System.out.println("instance variable is:"+obj.b);
}
}

For instance, variable if we do not create an object it gives an error. Let's see
public class Variables{
static int a=10;
int b=20; // In this program no object create for instance variable
public static void main(String args[]){
int c=30;
System.out.println("static variable is:"+a);
System.out.println("local variable is:"+c);
System.out.println("instance variable is:"+b);
}
}
Arrow mark show error when no object create for instance variables





Tuesday, January 12, 2021

Java Identifier

 Java Identifier 

These are the names the programmers choose. These names can be assigned to variables, methods, functions, classes, etc., to uniquely identify from a compiler. Java keywords can not be used as an identifier.

Rules for Java Identifier:

1. The first character of an identifier must be a letter, an underscore(_), or a dollar sign($).
2. The rest of the characters in the identifier can be a letter, underscore, dollar sign, or digit. Note that spaces are not allowed in identifiers.
3. Identifier cannot match any of Java's keywords.
4. Identifiers are case-sensitive. This means that age and Age are different identifiers.

Valid Identifiers:

int a;
int hi_java;
int value4;
int $height;

Invalid Identifiers:

int hi java;  // uses a space
int 4value;  //start with digit
int for;   // for is keyword
int #height;  //does not start with dollar sign

Program for identifiers.

public class Identifier{
public static void main(String args[]){
int a=10;
int hi_java=12;
int value4=15;
int $height=33;
System.out.println("result is="+a);
System.out.println("result is="+hi_java);
System.out.println("result is="+value4);
System.out.println("result is="+$height);
}
}


Let's discuss our program and find identifiers:-

Identifier // which is the class name 
main  // main is method name
int a=10;
int hi_java=12;
int value4=15;
int $height=33;
String // Predefined class name
args  // variable name



simple java program to print hello world

 simple java program to print hello world.

class Sample {
public static void main(String []args) {
      System.out.println("Hello World"); // prints Hello World
   }
}
In java programming file saving, file compiling, and run is different from any other languages. Therefore follow these step for the writing program:-
Step 1:- First open the notepad and add the code as above.


Step 2:- Save the file as Sample.java
Step 3:- Open the command prompt window and go to the directory where you saved the file. Assume it is in C:\>
then type javac Sample.java and press enter for compile. If there are no errors in your code then it cmd take to the next line.
Step 4: Then type java Sample then it gives output hello world.
3 and 4 show step 3 and 4 respectively
Let's discuss our code:-
The first line of our code is 
class Sample{
The word class is a keyword to define a new class and Sample is the name of the class. The class definition must begin with opening curly braces({) and end with closing curly brace(}). The rest of the things defined inside these braces are called members of the class.
The next line of the code in our code is
public static void main(String []args)
This line tells the computer that this is the main part of the program or also called the main() method of our program. At this point execution start by calling the main() method.
The keyword public indicates it is globally accessible.
The keyword static ensures it is accessible even though no object of the class exist.
The keyword void indicates it does not return a value.
The next line of our code is 
System. out.println("Hello World"); // prints Hello World
The system is the name of a standard class that contains the object that encapsulates the standard I/O devices for your system--the keyboard for command-line input and command-line output to the display. It is contained in the package java.lang, so it is always accessible just using the simple class name System.
out is the object that represents the standard output stream- the command-line on your display screen. out is the static variable. This means that out exists even there is no object of class System. System. out-references the out member of the class.
println("Hello World"), calls the println() method that belongs to the object out. It outputs the text string that appears between the parentheses to the command line.
// it shows comments in our code.





Sunday, January 10, 2021

Why java is platform independent language?

Why java is platform independent language?


Java is portable programming language and that java programs can run on any operating system. Java is a high level programing language that allows a developer to write programs that are independent of a particular type of computer. High level programming language are easier to read ,write ,and maintain it. But their code must be translated by compiler into machine language. Machine language is referred as machine code or object code. Machine language is collection of binary bits that a computer read and interprets it. This machine code is not human readable code. This machine code change by compiler of different operating system.

Therefore if we write a program(file name.c) in c language that is human readable format and machine can not understand this source code. For this situation this code must be converted into machine code. Then we convert this source code into machine code or machine language with the help of compiler. Compiler convert this code into machine code that is executable(file name.exe) .

1. First image show source code.

2. Second Image show after compile source code create a executable code or machine code or object code.

source code of c program

arrow mark show machine code(object code)
If this object code create by window operating system then it can not run in any other operating system.
But in Java programming language first JVM(java virtual machine) must be install in any type of operating system. 
When we write a program in java then this source code compile convert source code(AddTwoNumbers) into .class file or bytecode ( AddTwoNumbers.class) .This byte code is not a machine code. This bytecode execute with the help of JVM and then we get output.
arrow mark show the bytecode







 


Data Types in Java

 Data Types in Java Data Type in Java is defined as that specifies the size of memory allocated for a specific variable and which type of va...