JAVA

CORE JAVA NOTES

Introduction Of Java

  • Java technology is both a programming language and a platform. The Java programming language is a high-level language.
  • In the Java programming language, all source code is first written in plain text files ending with the .java extension.
  • Those source files are then compiled into .class files by the javac compiler.
  • A .class file does not contain code that is native to your processor; it instead contains bytecodes the machine language of the Java Virtual Machine (Java VM).
  • The java launcher tool then runs your application with an instance of the Java Virtual Machine.

History Of Java
  • James Gosling - founder of java.
  • Java is a high-level programming language originally developed by Sun Microsystems and released in 1995.
  • Java team members (also known as Green Team), initiated a revolutionary task to develop a language for digital devices such as set-top boxes, televisions etc.
  • For the green team members, it was an advance concept at that time. But, it was suited for internet programming. Later, Java technology as incorporated by Netscape.
  • Currently, Java is used in internet programming, mobile devices, games, e-business solutions etc. There are given the major points that describes the history of java.
Java Version History
There are many java versions that has been released.
  • JDK Alpha and Beta (1995)
  • JDK 1.0 (23rd Jan, 1996)
  • JDK 1.1 (19th Feb, 1997)
  • J2SE 1.2 (8th Dec, 1998)
  • J2SE 1.3 (8th May, 2000)
  • J2SE 1.4 (6th Feb, 2002)
  • J2SE 5.0 (30th Sep, 2004)
  • Java SE 6 (11th Dec, 2006)
  • Java SE 7 (28th July, 2011)

Java IDE Toolsi) Forte by Sun MicroSystems
ii) Borland Jbuilder
iii) Microsoft Visual J++
iv) WebGain Caf�
v) IBM Visual Age for Java
vi) Eclipes

Java Applications
There are mainly 4 type of applications that can be created using java:-

1) Standalone Application/Desktop Application
2) Web Application
3) Enterprise Application
4) Mobile Application
1) Standalone Application/Desktop Application :-
It is also known as desktop application or window-based application. An application that we need to install on every machine such as media player, antivirus etc. AWT and Swing are used in java for creating standalone applications.
2) Web Application :-
An application that runs on the server side and creates dynamic page, is called web application. Currently, servlet, jsp, struts, jsf etc. technologies are used for creating web applications in java.
3) Enterprise Application :-
An application that is distributed in nature, such as banking applications etc. It has the advantage of high level security, load balancing and clustering. In java, EJB is used for creating enterprise applications.
4) Mobile Application :-
An application that is created for mobile devices. Currently Android and Java ME are used for creating mobile applications.
Features Of Java
There is given many features of java. They are also known as java buzzwords.
1. Object-Oriented
2. Platform independent
3. Simple
4. Secured
5. Robust
6. Architecture neutral
7. Portable
8. Dynamic
9. Interpreted
10. High Performance
11. Multithreaded
12. Distributed


1. Object Oriented:-
Object means a real word entity such as pen, chair, table etc. Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts:-
i) Object
ii) Class
iii) Inheritance
iv) Polymorphism
v) Abstraction
vi) Encapsulation

2. Platform independent:-
Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.

3. Simple:-
Java was designed to be easy for the professional programmer to learn and use effectively. If you already understand the basic concepts of object-oriented programming, learning Java will be even easier.

Best of all, if you are an experienced C++ programmer, moving to Java will require very little effort. Because Java inherits the C/C++ syntax and many of the object-oriented features of C++, most programmers have little trouble learning Java.

4. Secured:-
Java is secured because:
i) No explicit pointer
ii) Programs run inside virtual machine sandbox.

5. Robust:-
Robust simply means strong. Java uses strong memory management. There are lack of pointers that avoids security problem. There is automatic garbage collection in java. There is exception handling and type checking mechanism in java. All these points makes java robust.

6. Architectural- neutral:-
There is no implementation dependent features e.g. size of primitive types is set.

7. Portable:-
We may carry the java bytecode to any platform.

8. Dyanamic:-
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. This makes it possible to dynamically link code in a safe and expedient manner. This is crucial to the robustness of the Java environment, in which small fragments of bytecode may be dynamically updated on a running system.

9. Interpreted:-
As described earlier, Java enables the creation of cross-platform programs by compiling into an intermediate representation called Java bytecode. This code can be executed on any system that implements the Java Virtual Machine. Most previous attempts at cross-platform solutions have done so at the expense of performance.

10. High Performance:-
Java is faster than traditional interpretation since byte code is "close" to native code still somewhat slower than a compiled language (e.g., C++)

11. Multi-threaded:-
A thread is like a separate program, executing concurrently. We can write Java programs that deal with many tasks at once by defining multiple threads. The main advantage of multi-threading is that it shares the same memory. Threads are important for multi-media, Web applications etc.

12. Distributed:-
We can create distributed applications in java. RMI and EJB are used for creating distributed applications. We may access files by calling the methods from any machine on the internet.

Object-Oriented Programming Concepts

Object means a real word entity such as pen, chair, table etc. Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts:-
i) Object
ii) Class
iii) Inheritance
iv) Interface
v) Polymorphism
vi) Abstraction
vii) Encapsulation

i) Object:- An object is a software bundle of related state and behavior.

Any entity that has state and behavior is known as an object. For example: chair, pen, table, keyboard, bike etc. It can be physical and logical.

ii) Class:- Collection of objects is called class. It is a logical entity.

In the real world, you'll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles. A class is the blueprint from which individual objects are created.

iii) Inheritance:- Different kinds of objects often have a certain amount in common with each other. 

Object-oriented programming allows classes to inherit commonly used state and behavior from other classes.

When one object acquires all the properties and behaviours of parent object i.e. known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.

iv) Interface:- The interface is a mechanism to achieve fully abstraction in java. There can be only abstract methods in the interface. It is used to achieve fully abstraction and multiple inheritance in Java. Interface also represents IS-A relationship. It cannot be instantiated just like abstract class.

v) Polymorphism:- When one task is performed by different ways i.e. known as polymorphism. For example: to convense the customer differently, to draw something e.g. shape or rectangle etc.

In java, we use method overloading and method overriding to achieve polymorphism.

vi) Abstraction:- Abstraction refers to the act of representing essential features without including background details or exceptions. Abstraction refers to the ability to make a class abstract in Object oriented programming. 

vii) Encapsulation:- The wrapping up of data and methods into a single unit (called class) is known as Encapsulation. Encapsulation is one of the four fundamental OOP concepts. The other three are inheritance, polymorphism, and abstraction. 









Data types in Java


There are majorly two types of languages. First one is Statically typed language where each variable and expression type is already known at compile time.Once a variable is declared to be of a certain data type, it cannot hold values of other data types.Example: C,C++, Java. Other, Dynamically typed languages: These languages can receive different data types over the time. Ruby, Python
Java is statically typed and also a strongly typed language because in Java, each type of data (such as integer, character, hexadecimal, packed decimal, and so forth) is predefined as part of the programming language and all constants or variables defined for a given program must be described with one of the data types.
The main purpose of Data Types in java is to determine what kind of value we can stored in to the variable.
Ex: int x;
like, int=12;
Java has two categories of data:
  • Primitive data (e.g., number, character)
  • Object data (programmer created types)

Primitive data
    Primitive data are only single values; they have no special capabilities. There are 8 primitive data types


      boolean
      boolean data type represents only one bit of information either true or false . Values of type boolean are not converted implicitly or explicitly (with casts) to any other type. But the programmer can easily write conversion code.



        byte
        The byte data type is an 8-bit signed two’s complement integer.The byte data type is useful for saving memory in large arrays.
        • Size: 8-bit
        • Value: -128 to 127
      short
      The short data type is a 16-bit signed two’s complement integer. Similar to byte, use a short to save memory in large arrays, in situations where the memory savings actually matters.
      • Size: 16 bit
      • Value: -32,768 to 32,767 (inclusive)
      int
      It is a 32-bit signed two’s complement integer.
      • Size: 32 bit
      • Value: -231 to 231-1
      Note: In Java SE 8 and later, we can use the int data type to represent an unsigned 32-bit integer, which has value in range [0, 232-1]. Use the Integer class to use int data type as an unsigned integer.
      long:
      The long data type is a 64-bit two’s complement integer.
      • Size: 64 bit
      • Value: -263 to 263-1.
      Note: In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1. The Long class also contains methods like compareUnsigned, divideUnsigned etc to support arithmetic operations for unsigned long.
      Floating point Numbers : float and double 
      float
      The float data type is a single-precision 32-bit IEEE 754 floating point. Use a float (instead of double) if you need to save memory in large arrays of floating point numbers.
      • Size: 32 bits
      • Suffix : F/f Example: 9.8f
      double: 
      The double data type is a double-precision 64-bit IEEE 754 floating point. For decimal values, this data type is generally the default choice.
      Note: Both float and double data types were designed especially for scientific calculations, where approximation errors are acceptable. If accuracy is the most prior concern then, it is recommended not to use these data types and use BigDecimal class instead.
      Please see this for details: Rounding off errors in Java
      char
      The char data type is a single 16-bit Unicode character. A char is a single character.
      • Value: ‘\u0000’ (or 0) to ‘\uffff’ 65535
      Java Variables

      The Java programming language defines the following kinds of variables:

      i) Instance Variables (Non-Static Fields)
      ii) Class Variables (Static Fields)
      iii) Local Variables

      i) Instance Variables (Non-Static Fields):- Technically speaking, objects store their individual states in "non-static fields", that is, fields declared without the static keyword.

      Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words); the currentSpeed of one bicycle is independent from the currentSpeed of another.

      ii) Class Variables (Static Fields):- A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated.

      A field defining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number of gears will apply to all instances. The code static int numGears = 6; would create such a static field. Additionally, the keyword final could be added to indicate that the number of gears will never change.

      iii) Local Variables:- Similar to how an object stores its state in fields, a method will often store its temporary state in local variables. The syntax for declaring a local variable is similar to declaring a field (for example, int count = 0;).

      There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared � which is between the opening and closing braces of a method.

      As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class.
      Arrays
      Array is a collection of similar type of elements that have contiguous memory location.

      Array is an object the contains elements of similar data type. It is a data structure where we store similar elements. We can store only fixed elements in an array.

      Array is index based, first element of the array is stored at 0 index.

      Creation of arrays:
      Arrays are data structures that can store large amount of information of the same data type grouped together and known by a common name. Each member is called an element of the array.

      Arrays are capable of storing primitive data types as well as objects. The elements of the array can be accessed by its index value that starts from 0. Once array is declared, its size cannot be altered dynamically.

      Arrays can be :-
      a) declared and later assigned or
      b) initialized.
       
       // declaration of an array
      int subject[ ] = new int[ 10 ] ; 
      
       // if not assigned, default 0 is assigned for int element 
      System.out.println( subject[ 1 ] ) ;
      
       // assigning a value to an element
      subject[ 1 ] = 45 ;     
      
       // now, prints 45
      System.out.println( subject[ 1 ] ) ;  
      
      

      Advantages of Array 
      1. Code Optimization: It makes the code optimized, we can retrive or sort the data easily.
      2. Random access: We can get any data located at any index position.

      Disadvantage of Array
      Size Limit: We can store only fixed size of elements in the array. It doesn't grow its size at runtime. To solve this problem, collection framework is used in java.

      Types of Array
      There are two types of array.
      1. Single Dimensional Array
      2. Multidimensional Array

      Example:-Single Dimensional Array
      //Start of ArrayDemo class 
      class ArrayDemo
      {
       //Start of Main Method
       public static void main(String args[])
       {
         //declaration and instantiation
         int array[]=new int[6];
         
         //initialization
         array[0]=100;
         array[1]=200;
         array[2]=300;
         array[3]=400;
          array[4]=500;
         array[5]=600;
      
       //length is the property of array
       for(int i=0;i< array.length;i++)
           
       //printing array
       System.out.println(array[i]);
       
       }//End of Main Method
       
      }//End of ArrayDemo class
      
      Save this file as ArrayDemo.java
      
      To compile: javac ArrayDemo.java
      To execute: java ArrayDemo
      
      Output:- 
        100
        200
        300
        400
        500
        600
       
      

      Example:-Multidimensional Array
      //Start of MultiArray class 
      class MultiArray
      {
       //Start of Main Method
       public static void main(String args[])
       {
        //declaring and initializing 2D array
        int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
      
        //Start of outer for loop
        for(int i=0;i< 3;i++)
        {
        //Start of inner for loop
         for(int j=0;j< 3;j++)
          {
           //printing 2D array
         System.out.print(arr[i][j]+" ");
         
         }//End of inner for loop
         System.out.println();
         
        }//End of outer for loop
        
       }//End of Main Method
       
      }//End of MultiArray class 
      
      Save this file as MultiArray.java
      
      To compile: javac MultiArray.java
      To execute: java MultiArray
      
      Output:- 
        1 2 3
        2 4 5
        4 4 5
      

      Printing Variables  
      System.out.println(" text " ); will print what is between the double quotes “ “ and move the printing cursor to the next line.
      System.out.print ( “ text ”); will print what is between the double quotes and leave the printing cursor on the same line.

      The basic output statement in Java is
                                        System.out.println ( );  
      Operators in Java
      An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. Operators are used in programs to manipulate data and variables. Java supports a rich set of operators.

      1. Arithmetic Operators
      2. Relational Operators
      3. Logical Operators
      4. Assignment Operators
      5. Increment and Decrement Operators
      6. Conditional Operators
      7. Bitwise Operators
      8. Special Operators

      1. Arithmetic Operators:- Different arithmetic operators supported by java language are listed below:-

      Symbol Used

      Operation Performed
      +
      Addition
      -
      Subtraction
      *
      Multiplication
      /
      Division
      %
      Modulus

      2. Relational Operators:- These operators are used to compare the values of the operands that must be compitable, in order to facilitate decision-making.
      Java supports 6 relational operators are:-


      Symbol Used

      Operation Performed
      <
      Less than
      <=
      Less than or equal to
      >
      Greater than
      >=
      Greater than or equal to
      ==
      Equal to
      !=
      Not Equal to

      3. Logical Operators:- Java has the following three logical operators.


      Symbol Used

      Operation Performed
      !
      Logical NOT
      &&
      Logical AND
      ||
      Logical OR

      The logical operators && and || are used when we want to test more than one condition and make decisions.
      Ex:-
      i) if(a>b && x==10)
      ii) if(age>55 && salary<2000)

      4. Assignment Operator :- Assignment operators are used to assign the result of an expression to a variable.
      Assignment Operator:- '='
      Ex:-
      c = a+b.

      5. Increment / Decrement Operator:- Java supports two very useful operators increment (++) and decrement operator (--), these are not generally found in other languages. The increment operator adds 1 from the operand, while decrement operator subtracts 1 from the operand. Both are unary operators and takes the following form:-

      ++m (pre incrementation)      or     m++ (post incrementation)
      --m (pre decrementation)      or       m-- (post decrementation)

      6. Conditional Operator (?:) :- A ternary operator pair "?:" is available in the java to construct conditional expression of the form

      Exp1 ? Exp2 : Exp3

      This the only operator that take 3 operands
      Result = Testing Expression : Exp1 : Exp2

      If Testing Expression is True perform expression 1 and assign to result. If Testing Expression is False perform expression 2 and assign to resut

      7. Bitwise Operator:-These operator are used for testing the bits or for shifting them right or left. Bitwise operators may not be appiled to float or double.
      Different bitwise operators supported by Java Language are listed below are:-

      Symbol Used

      Operation Performed
      >>
      Shift-right
      <<
      Shift-left
      >>>
      Shift-right with zero fill
      -
      One's complement
      &
      Bitwise AND
      !
      Bitwise OR
      ^
      Bitwise Exclusive OR

      8. Special Operator:- Java supports some special operators such as instanceof operator, Dot operator.

      Instanceof: This operator is used only for the obect refernce variables. The operator checks wether the object is of a pirticular type (class type or interface type). instanceof operator is written as:
      (Object reference variable) instanceof (class/ interface type)

No comments:

Post a Comment