Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

What is a Class & Object?

What is a Class & Object?
A Class is a user defined datatype. Class is a structure representing the data & it's related function.


Class will act as the base for Encapsulation & we would be implementing the concept of Encapsulation through Object.

(NOTE):
Any java application looks like collection of classes.(In java everything should be present in the form of an encapsulated manner, thus everything should present inside the class nothing should present inside the Class nothing should present outside the Class.

But whereas "C" application looks like collection of function.


Object is an instance of a Class. The Memory space allocated for the members of a Class dynamically at the runtime is known as Object.

Object would be representing the data of a Class in the RAM.

What is Operators,Operand & Expression?

What is Operators,Operand & Expression?
Operators : Java operators use to perform specific operations on one or more than one operands and then return a result. Example :-  ( + ),( - ) etc.




Operand is a term used to describe any object that is manipulated by an operator.
Example : -

int  num = 15 + 12;
int another in = 15-12;

Then + or - are the operator, and 15 and 12 are the operands. We can use variables as well instead of literals are also operands.


An expression is formed by combining variables, literals, method return values and operators.

In the line below, 15 + 12 is the expression which return 27 in this case.

int myValue = 15 + 12;

Char and Boolean primitive type

Char and Boolean primitive type
A char is used to store single character  and occupies two bytes of memory, or 16 bits. As char store single character but still  occupies two bytes. The reason is that it allows you to store Unicode characters.


Unicode
:- Unicode is universal encoding standard for different languages, Each character or letter, number, or symbol is assigned a unique value that applicable across any programming language.

In the English alphabet, we have the letters A through Z. Meaning only 26 characters are needed in total to represent the entire English alphabet. But other languages need more characters, and often a lot more.

Unicode allows us to represent these languages and the way it works is that by using a combination of the two bytes that a char takes up in memory.

package com.ekumeedhelp;

public class CharType {

public static void main(String[] args) {
char charValue = 'A';
char unicodeValue = '\u0045';
System.out.println(charValue);
System.out.println(unicodeValue);

}

}



Boolean :- A boolean value is either True or False, Yes or No, 1 or 0. In Java terms we have a boolean primitive type and it can be set to two values only. true or false They are actually pretty useful and you will use them a lot when programming

package com.ekumeedhelp;

public class BooleanType {

public static void main(String[] args) {

boolean trueBooleanValue = true;
System.out.println(trueBooleanValue);

boolean falseBooleanValue = false;
System.out.println(falseBooleanValue);

}

}

Float and Double primitive type.

Float and Double primitive type.
Floating number : - Unlike Integer number floating number has fraction part or decimal. Ex - 3.23434


We used floating when more precision is needed. In java we have two primitive type number to represent floating number are Float and Double. Float is single precision number while double s double precision number. Here precision means format and size(Space). 

Now you know float is single precision and it occupies 32 bits space where as Double is double precision and occupies 64 bits.


package com.ekumeedhelp;

public class PrimitiveDataType2 {

public static void main(String[] args) {

float minFloatValue = Float.MIN_VALUE;
float maxFloatValue = Float.MAX_VALUE;

System.out.println("Min Float Value : " + minFloatValue);
System.out.println("Max Float Value : " + maxFloatValue);

double minDoubleValue = Double.MIN_VALUE;
double maxDoubleValue = Double.MAX_VALUE;

System.out.println("Min Double Value : " + minDoubleValue);
System.out.println("Max Double Value : " + maxDoubleValue);

}

}

Variables & Data Type in Java

Variables & Data Type in Java


Variables :-
 Variable are a way to store information in our computer random  access memory(RAM). A variable means that can be changed. Java support different type of variables like int, long, double etc.. collectively  these are knows as Data Type.

While declaring variable we must have to tell the type of variable to computer. Lets start defining a variable to store whole number without decimal.

Syntax to define variable 

<Type> <Variable Name> = <value>

int myFirstNumber = 10;

Here int is the type of variable in Java which mean integer, that store whole number and myFirstNumber is the variable name which is used to access the variable and finally assigned 10 value into myFirstNumber variable.


Data Type :- In Java  data type in divided into two categories.
  1. Primitive
  2. Non-Primitive 
Total we have 8 primitive type(int,short,long,byte,float,double,boolean,char).

 Data Type     Size 
 boolean     1 bit
 char                     2 byte              
 byte 1 byte
 short 2 byte
 int 4 byte
 long 8 byte
 float 4 byte
 double 8 byte

package com.ekumeedhelp;

public class PrimitiveDataType1 {

public static void main(String[] args) {
int minIntValue = Integer.MIN_VALUE;
int maxIntValue = Integer.MAX_VALUE;

System.out.println("Min Int Value : " + minIntValue);
System.out.println("Max Int Value : " + maxIntValue);

short minShortValue = Short.MIN_VALUE;
short maxShortValue = Short.MAX_VALUE;

System.out.println("Min Short Value : " + minShortValue);
System.out.println("Max Short Value : " + maxShortValue);

byte minByteValue = Byte.MIN_VALUE;
byte maxByteValue = Byte.MAX_VALUE;

System.out.println("Min Byte Value : " + minByteValue);
System.out.println("Max Byte Value : " + maxByteValue);

long minLongValue = Long.MIN_VALUE;
long maxLongValue = Long.MAX_VALUE;

System.out.println("Min Long Value : " + minLongValue);
System.out.println("Max Long Value : " + maxLongValue);

}

}

Test

Hello Java program

Hello Java program
In this post we will learn about Hello Java program and try to understand with example.

1
2
3
 public class Hello {

}

Like other programming language Java programs also have keywords. Each keywords has specific meaning and sometime used in some specific orders. keywords are case sensitive like public and Public and even PUBLIC all are totally different.

With the help of keywords and certain rule we can write statements which collectively form java program.

In the above program you can see public and class are two keywords in green and Hello is the name of the class. lets try to understand the meaning of both keywords.

public :- In java programming public is the access modifier is allow us to define the scope or you can also say how other part of the program access this code. For example  public bus that means it is available for everyone i.e scope is for everyone. So here our class is public means available for every other class.

class :- To define a class we need the class keywords. In above example Hello is the class name and defined using class keyword and whatever you can write withing both left and right curly braces are called as class body.  


 

Java Tutorial for beginners

Java Tutorial for beginners
Hi Guys, Welcome to the Java tutorials for beginners.In this tutorials series you will learn core Java concept in depth. To join free Java class join my telegram channel and also subscribe my YouTube channel using below link. 


 What is Java?

  • Java is a programming language was developed by Sun Microsystems in 1995.
  • James Gosling is known as the father of Java.
  • Java is high level programming language.
  • Java is object oriented programming language.
  • Object-oriented programming (OOPs) is a methodology or rules
  • OOPs concepts are: [Class, Object, encapsulation, Abstraction, polymorphism, Inheritance]

Java JDK and eclipse installation and setup

In this post we you will learn how to install and setup Java 11 & 14 and eclipse software in 64 bit windows 10 machine.

To start programming in java you must have Java install in your machine.
Download JDK 14 or JDK 11 as per your your machine like windows or linux and check out the video and follow the step.


How to setup eclipse in windows 10 machine.

 


Java serialisation rule

Java serialisation rule
A static variable is not a part of object state and hence it won't participate in serialization due to this declaring static variable as transient there is no use.


Every final variable get value at compile time where as normal variable get value at run-time.


Final variable will be participated in serialisation directly by value hence declaring a final variable as transient there is no impact.


We can serialized any number of file but in which order we have seralized in the same order we have only to deseralized.
i.r serialization and deseralization order must be maintain.


Order of serilalization would be like this,if you are not aware of this

Object o = ois.read()

if(o instanceOf Dog)
Dog d = (Dog)o

if(o instanceOf Cat)
Cat d = (Cat)o

if(o instanceOf Rat)
Rat d = (Rat)o


class Dog {
Dog d = new Dog();
}
Class Cat {
Rat r = new Rat();
}
Class Rat {
 int j =0;
}

In serialization, everything is controlled by JVM, not a programmer, It is also possible to save the total object to the file and it is not possible to save part of the object, which may create performance problem to overcome this problem we should go for externalization.The main advantage of externalization over serialization is everything is taken care by programmer and JVM doesn't have any control based on our requirement we can save total or part of the object which improved system performance.

To provide the externalizable ability for any java class must have to implement the Externalizable interface.That interface has two methods WriteExternal and ReadExternal


The Externalizable(1.1) interface is the child method os serializable(1.1) interface.

Account object has so many properties i want to do serialization  externalization





Case 1: =

If a child doesn't implement serializable we can serialize child class object if parent class implement serializable interface.i.e serializable nature is inherited from parent to child.
hence if the parent is serializable every child is serializable.

Object class doesn't implement serializable interface.

Case 2:=

Even though parent class doesn't implement serializable we can serialize child class if child implements serializable interface.

At the time of serialization, JVM will check if any variable inheriting from a non-serializable parent or not if yes the JVM ignores the original value and save default value to the file.

At the time of deserialization, JVM will check if any parent class non-serializable or not if any parent is non-serializable then JVM will execute instance control flow for every and share instance variable to the current object

Every non-serializable should compulsorily contain no argument constructor it may be default constructor generator to compile or customized constructor explicitly provided by programmer otherwise we will get a runtime constructor invalid class exception.