WORKING WITH JAVA
Java is an object oriented programming language from Sun Microsystems. Developed by team headed by James Gosling.
à Java is more secure.
à Java is Portable
à Machine and Platform independent (When we compile java code, it convert java file to byte code (class file) rather than machine code, byte code is machine and platform independent (.class file). We can interpret these class file to any machine having JVM, JVM is used to interpret class file and convert it to machine dependent code and then execute it.
Variables in Java
Variable/Fields in java are declared in classes. Variables are used to hold data.
Statement: In java each line of code is called statement ended with semicolon.
Comments: We can make single line comment // or multiline comment using /* */
Identifier: Variable, Class, Method name are called identifier, can be alpha-numeric with .$_ but must start with _ or alphabets
Constants: We can make a variable constant by using final keyword.
Naming Conventions in java
1à Class name should be start in Upper Case.
2à Variable and method name must be start with small letter.
3-> If we have composite method or variable name, we can use small letter for first word and capital letter for second word.
4-> Method name should have () doesn’t matter arguments are given or not.
Package: Package can be defined as the collection of sub package and class file
Packages are use to group similar class files together.
We have lot of inbuilt package, we can import them and use methods of the class
We can create our own packages as well.
Class: Wrapping up of data and function in a single unit is called class.
In a class we have can fields/variables, Constructors, Methods.
All Java classes of subclasses of Object Class so all java classes inherits methods of Object class.
Object class is grandfather of all available classes in java( need not to inherit programmatically)
Variables: are used to hold data.
Methods: can be defined as the group of commands, which are placed in a methods to perform a specific task.
Constructor: are the special kind of method, which is used to initialize the attributes of the class.
è Constructor name is same as class name.
è There is no return type of the constructor.
è Constructors are called automatically when the object is created.
Variable and Methods in combined called, class members.
DATA TYPES IN JAVA
Numeric Integer Values: Byte (1 Byte) Short (2 Bytes) Int (4 Bytes) Long (8 Bytes)
Numeric Float Values: Float (4 Byte) Double (8 Bytes)
Boolean: True/False
String: Use to hold character strings
In Java, String is a class, we create object of string class, that’s why we can use different methods with string object belongs to string class.
- String is a class in java
- When we declare String string1 = “Hello”
It is similar to String string1 = new String(“Hello”);
As we know String is a class so we have large number of methods in this class these methods can be use by any object of string class, here string1 is an object of string class so we can use methods with this object.
DATA TYPE CONVERSION IN JAVA
*** Values can be converted into integer from string by Integer.parseInt(value)
Here we are using parseInt static method of Integer classs.
*** Values can be converted into String by String.valueOf(value)
Here we are using valueOf static method of string class.
In Numeric Conversion :
è We need not to convert small data type to bigger one
If we assign short to int, it will done automatically
If we assign byte to short, it will done automatically
If we assign int to double, it will done automatically
If we assign int to float, it will done automatically
If we assign float to double, it will done automatically
But Not Work, it we assign higher size value to lower size it will not work automatically, we need to do it explicitly
If we assign double to float, it will done manually =float(double value)-some loss
If we assign float to int, it will done manually = int(float value)-some loss
If we assign int to short, it will done manually =short(int value)-some loss
Instance & Static Variables
Instance Variable: By default the variable which is created in a class are instance variable, whenever object of class is created it makes a copy of the variable.
For example if we have 2 variables A,B in a class, now if I create 1 object=Obj1
1 copy of both (A & B) variable is created, now if I create 1 more object means 1 more copy of variable is created. So we can say that for each object we would have a set of variable copies, if we have 10 objects of a class, then we will have 10 copies of variable (A& B){ instance variable have separate copy for each object)
Static variable (Class Variable)
We can create static variable by using static keyword, while creating variable.
Static variable do not need any object we can call static variable directly by class name.
In case of static variable we would have single copy of variable regardless that any number of objects for that class. All objects of class will have access to same copy of variable.
- Static variable is common for all objects so rather than calling it by variable name we call is by using class name.
Instance and Static Methods
Instance Method is same as Instance variable for each object is will have a separate copy of method working.
We can call the instance method by creating object and then object.Method name
Static Method are the methods which is common to complete class, it will be common to all objects of that class. So that’s why rather than calling it with a object name we call it by using class name.
********************* IMPORTANT *************************
Instance method can access Static variables BUT
Static method can’t access to Instance variable. SO we can say that Static method of a class can use only Static variables of class.
OOPS CONCEPTS
Polymorphism: is the same name multiple use, Overloading and Overriding is the example of polymorphism.
Overloading: If a class have methods with same name but different arguments (different ordering of data type or different number of arguments), it is called overloading.
Calling a function will completely depend upon number of arguments passing.
Overriding: If a parent and child class have same name methods with same arguments this is called overriding.
Inheritance: By using inheritance we can transfer properties of one class to another class.
Inheritance can be implemented by using extends keyword.
Class which is inherited is called Parent Class or Super Class.
Class which inherits the other class is called subclass.
If we create any other class object without inheritance it is called non sub class.
Array: Array is used to hold similar kind of multiple data.
Array can be declared like
Datatype name []= new Datatype[2]; Or Datatype [] name
- In square brackets we define size of the array, according to size, index in array are set to 0 to (size-1)
Int intArr[]= new Int[2] // Here we define size of array.
OR
To create a array with initial value(in this case size will be defined automatically)
Int intArr[] = {2,3,5,6}
- To create a multi-dimensions array
Int intArr[][]= {
{2,3,5,6},
{26,9,0}
};
è To access element of the array arrayname[passindex]
è To find length of array : intArr.length
CLASS & OBJECT
Class: Wrapping up of data and functions in a single unit is called class.
Class can be defined as of 2 types
Concrete Class Abstract Class
Having concrete methods only having at least 1 abstract method
Can create object and inherit as well Cant create object just inherited
Methods
Abstract Concrete
Methods without body(just declared) Method with body
Interface
The collection of abstract methods only.
Used for multiple inheritance, can be inherited using implements keyword
Package
Package is used to hold similar type of class, sub packages etc.
We can place a class in a package by writing in first line
To create a package package org.abc.cdf ;
We can use classes of other packages by using import keyword.
import org.abc.def ;
Java has lot of predefined methods in classes placed in different packages, we can use that build in classes by importing these packages.
Java.io.* : -- related to all methods(classes) for input/ output file handling.
Java.sql.* :- related to working with JDBC.
And many more available.
By default all java class is import java.lang package which include all basic usability classes like System
We have few packages which are not specifically designed for java like
Javax.swing.*
This package is language independent(not for java only)
Nested Class : Class inside another class.
A class can have
à Variable / Fields
à Constructors
à Methods
Fields and Methods are combined is called Class Members.
OBJECT
Object can be defined as an instance of class.
With Object we can access class members.
Object is similar like variable; it’s a variable of type class.
THIS KEYWORD
This keyword is used to define which variable is a member variable of class and which one is the local variable.
We can use This keyword with Methods and Variable.
This define which variable/method is a member variable of class. For this we place this.variablename it shows , it’s a member variable of class.
EXCEPTION HANDLING IN JAVA
In java exception can be handled by try-catch, we can put code which may throw an exception in try block and when an exception occur, execution in try block is terminated and control transfer to catch block.
We can have many type of exception,
Numberformatexception : if datatype is not same.
Arrayindexoutofboundexception : if we have a[4], and we try to access value of a[5] which is out of the bound of the array.
And many more…………..
Finally : finally block is added after the try catch, code written in finally will be executed in any case, if the exception is thrown then finally will be executed after catch if not thrown then it will be executed after try.
Finally may be use for some patch up, like closing file in case of exception, destroying objects etc.
Whenever an exception error, it throw an error of exception, which is caught in catch and then we can use that object to display message etc.
Access Modifiers in Java
We can set access modifiers to Class, Method and Variable.
We have 4 types of access modifier,
1à Public : It can be accessed anywhere by any class.
2à Protected : It can be accessed in same class, sub class and same package.(even if sub class in different package)
3àPackage(Default) : It can be accessed anywhere in same package.
4à Private : It can be accessed only in same class.
No comments:
Post a Comment