Java basics in minutes

Java basics in minutesJava is widely used in every corner of world and of human life.

Java is not only used in software but is also widely used in designing hardware controlling software components.

There are more than 930 million JRE downloads each year and 3 billion mobiles phones run java.

DammnnBlockedUnblockFollowFollowingJan 30Following are some other usages of Java:Developing desktop application.

Web application like linkdein.

com, snapdeal.

com etc.

Mobile operating system like Android.

Embedded systems.

Robotics and games etc.

FEATURES OF JAVAThe prime reason behind creation of Java was to boring probability and security feature into a computer languages.

Besides these two major features, there were many other features that played an important role in moulding out the final form of this outstanding language.

Those features are:Simple: java is easy to learn and its syntax is quite simple, clean and easy to understand.

The confusing and ambiguous concepts of C++ are either left out in Java or they have been re-implemented in a cleaner way.

Object Oriented: In java everything is Object which has some data and behaviour.

Java can be easily extended as it is based on Object Model.

Robust: Java makes an effort to eliminate error prone codes by emphasising mainly on compile time error checking and runtime checking.

But the main areas which Java improved were Memory Management and mishandled Exceptions by introducing automatic Garbage Collector and Exception Handling.

Platform independent: Unlike other programming languages such as C, C++ etc which are complied into platform specific machines.

Java is guaranteed to be write-once, run anywhere languages.

On compilation JAVA program is compiled into byte code this byte code is platform independent and can be run on any machine, plus this byte code format also provide security.

Any machine with JAVA Runtime Environment can run JAVA Programs.

SECUREWhen it comes to security, JAVA is always the first choice.

With JAVA secure features it enables us to develop virus free, temper free system.

JAVA program always runs in JAVA runtime environment with almost null interaction with system OS, hence it is more secure.

MULTI THREADINGJAVA multithreading feature makes it possible to write program that can do many tasks simultaneously.

Benefits of multithreading is that it utilise same memory and other resource to execute multiple threads at the same time, like while typing, grammatical errors are checked along.

ARCHITECTURAL NEUTRALComplier generates byte codes, which have nothing to do with a particular computer architecture, hence a JAVA program is easy to interpret on any machine.

PORTABLEJAVA Byte code can be carried to any platform.

No implementation dependent features.

Everything related to storage is predefined, example: size of primitive data types.

HIGH PERFORMANCEJAVA is an interpreted language, so it will never be as fast as a complied language like C or C++.

But.

JAVA enables high performance with the use of just in time compiler.

WHAT IS JVM?Java virtual machine is a virtual machine that provides runtime environment to execute java byte code.

The JVM doesn’t understand JAVA typo, that’s why you compile your *.

java files to obtain *.

class files that contain the byte cord understandable by the JVM.

JVM control execution of every JAVA program.

It enables features such as automated exception handling, Garbage-collected heap.

JVM ARCHITECTUREClass loader: class loader loads the class for execution.

Method area: stored pre class structure as constant pool.

Heap: heap is in which objects are allocated.

Stack: local variable and partial results are store here.

Each thread has a private JVM stack created when the thread is created.

Program register: program register holds the address of JVM instruction currently being executed.

Native method stack: it contains all native used in application.

Executive engines: execution engine controls the execute of instructions contained in the method of the classes.

Native method interface: native method interface gives an interface between java code and native code during execution.

Native method libraries: native libraries consists of files required for the execution of native code.

DIFFERENCE BETWEEN JDK AND JREJRE: The JAVA runtime environment (JRE) provides the libraries, the JAVA virtual machine, and other components to run applets and application written in the JAVA programming language.

JRE does not contain tools and utilities such as complier or debuggers for developing applets and application.

JDK: The JDK also called Java Development Kit is a superset of the JRE, and contains everything that is in the JRE, plus tools such as the compilers and debuggers necessary for developing applets and applications.

FIRST JAVA PROGRAMCLASS: Class keyword is used to declare classes in JAVA.

Public: it is an access specifier.

Public means this function is visible to all.

Static: static is again a keyword used to make a function static.

To execute a static function you do not have to create an object of the class.

The main() method here is called by JVM, without creating any object for class.

Void: it is the return type, meaning this function will not return anything.

Main: main() method is the most important method in a java program.

This is the method which is executed, hence all the logo must be inside the main() method.

If a java class is not having a main() method, it causes compilation error.

String [] args: this represents an array whose type is String and name is args.

We will discuss more about array in Java Array section.

System.

out.

println: this is used to print anything on the console like printf in C language.

After writing your JAVA program, when you will try to compile it.

Compiler will perform some application operation on your program.

Once it is compiled successfully byte code (.

class file) is generated by the compiler.

After compiling when you will try to run the byte code (.

class file), the following steps are performed at runtime:-Class loader loads the java Class.

It is subsystem of JVM Virtual machine.

Byte code verifier checks the code fragments for ilea codes that can violates access right to the object.

Interpreter reads the byte code stream and then executes the instruction, step by step.

DATA TYPES IN JAVAJAVA language has a rick implementation of data types.

Data types specify size and the type of values that can be stored in an identifier.

In java, data types are classified into two categories:Primitive Data TypeNon-Primitive Data typePRIMITIVE DATA TYPEA primitive data type can be of eight types:CharBooleanByteShortIntLongFloatDoubleOnce a primitive data type has been declared its type can never change, although in most cases its values can change.

These eight primitive type can be put into four groups.

INTEGERThis group includes byte, short, int, longByte: it is 1 byte integer data type.

Value range from -128 to 127.

Default value zero.

Example: byte b=10.

Short: it is 2 bytes integer data type.

Value range from -32768 to 32767.

Default value zero.

Example short s=11.

Int: it is a 4 byte integer data type.

Value range from -2147483648 to 2147483647.

Default value zero.

Long: it is 8 byte integer data type.

Value range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Default value zero.

FLOATING POINT NUMBERThis group includes float, double.

Float: it is 4 bytes float data type.

Default value 0.

0f.

example: float ff=10.

3f;Double: it is 8 type float data type.

Default value 0.

0d.

example double db=11.

123;CHARACTERThis group represent char, which represent symbols in a character set, like letters and numbers.

Char: it is 2 byte unsigned unicode character.

Range 0 to 65,535BOOLEANThis group represent boolean, which is a special type of representing true/false values.

They are defined constant of the language.

Example: boolean b=true.

NON-PRIMITIVE DATA TYPEA reference data type is used to refer to an object.

A reference variable is declare to be of specific and that type can never be change.

We will talk a lot more about reference data type later in classes and object lesson.

IDENTIFIER IN JAVAAll JAVA components require names.

Name used for classes, methods, interface and variable are called identifier.

Identifier must follow some rules.

Here are the rules:All identifier must start with either a letter or currency character ($) or an underscore.

After the first character, an identifier can have any combination of characters.

A JAVA keyword cannot be used as an identifier.

Identifier in JAVA are case sensitive, foo and Foo are two different identifiers.

int x = 10;byte y = (byte)x;WIDENING OR AUTOMATIC TYPE CONVERSIONAutomatic Type casting take place when,The two type are compatible.

The target type is larger than the source type.

public class test {public static void main (String [] args) {int i = 100;long l = i; // no explicit type casting requiredfloat f = i; // no explicit type casting requiredSystem.

out.

println(“int value “ +i);System.

out.

println(“long value “ +l);System.

out.

println(“flaot value “ +f);}}NARROWING OR EXPLICIT TYPE CONVERSIONWhen you are assigning a larger type value to a variable of smaller type, then you need to perform explicit type casting.

Example:-public class Test {public static void main(String[] args) {double d = 100.

04;long l = (long)d; //explicit type casting requiredint i = (int)l; //explicit type casting requiredSystem.

out.

println(“Double value “+d);System.

out.

println(“Long value “+l);System.

out.

println(“Int value “+i);}}OutputDouble value 100.

04Long value 100Int value 100VARIABLEWHAT IS A VARIABLE?When we want to store any information, we store it in an address of the computer.

Instead of remembering the complex address where we have stored our information, we name that address.

The naming of an address is known as variable.

Variable is the name of memory location.

JAVA programming language defines mainly three kind of variables.

Instance variables.

Static variables.

Local variables.

INSTANCE VARIABLEInstance variable are variables that are declare inside a class but outside any method, constructor or block.

Instance variable are also variable of object commonly known as field or property.

They are referred as object variable.

Each object has its own copy of each variable and thus, it doesn’t effect the instance variable if one object changes the value of the variable.

class student {String name;int age;}STATIC VARIABLEStatic are class variables declared with static keyword.

Static variables are initialised only once.

Static variables are also used in declaring constant along with final keyword.

class student{String name;int age;static int institutecode = 1101;}Here instituteCode is a static variable.

Each object of student class will share instituteCode property.

Additional points on static variable:Static variable are also known as class variable.

Static means to remain constant.

In JAVA, it means that it will be constant for all the instance created for that class.

Static variable need not be called from object.

It is called by classname.

static variable name.

Example: suppose you make 2 objects of class Student and you change the value of static variable from one object.

Now when you print it from other object, it will display the changed value.

This is because it was declared static I.

e constant for every object created.

package testlab;class Student {int a;static int id = 35;void change () {System.

out.

println(id);}}public class testlab {public static void main (String [] args ) {student o1 = new student ();student o2 = new student ();o1.

change();student.

id = 1;o2.

change();}}LOCAL VARIABLELocal variable are declared in method, constructor or block.

Local variable are initialised when method, constructor or black start and will be destroyed once it end.

Local variable reside in stack.

Access modifiers are not used for local variable.

float getDiscount (int price) {float discount;discount = pirce * (20/100);return discount;}//Here discount is a local variable.

CONCEPT OF ARRAY IN JAVAAn array is a collection of similar data types.

Array is a container object that hold values of homogenous type.

It is also known as static data structure because size of an array must be specified at the time of its declaration.

An array can be either primitive or reference type.

It gets memory in heap area.

Index of array starts from zero to size-1.

FEATURES OF ARRAYIt is always indexed.

Index begins from 0.

It is a collection of similar data types.

It occupies a contiguous memory location.

ARRAY DECLARATIONSyntax:Datatype [] identifier;OrDatatype identifier [];Both are valid syntax for array declaration.

But the former is more readable.

Example:int [] arr;char [] arr;short [] arr;long [] arr;int [] [] arr; // two dimensional arrayINITIALISATION OF ARRAYNew operator is used to initialise an array.

Example:int [] arr = new int [10]; // this creates an empty array named arr of integer type whose size is 10orint [] arr = {10,20,30,40,50}; //this creates an array named arr whose elements are givenACCESSING ARRAY ELEMENTAn mention earlier array index starts from 0.

To access nth elements of an array.

Syntaxarrayname[n-1];Examples: to access 4th elements of a given arrayint [] arr = {10,20,30,40};System.

out.

println (“element at 4th place “ + arr[3]);.

. More details

Leave a Reply