
--Your friends at LectureNotes

Java Programming

by Namrita VarshneyNamrita Varshney
Type: NoteInstitute: APJ Abdul Kalam Technological University Course: B.Tech Specialization: Computer Science EngineeringOffline Downloads: 205Views: 4755Uploaded: 11 months ago

Share it with your friends

Suggested Materials

Leave your Comments


Namrita Varshney
Namrita Varshney
CHAPTER- 1 JAVA CORE Java: Java is a object oriented programming language and a platform. Java is a high level, platform independent, robust, secured and object-oriented programming language. Platform: Any hardware or software environment in which a program runs, is known as a platform. Since Java has its own runtime environment (JRE) and API, it is called platform. Feature of java: 1. Object-Oriented 2. Platform independent 3. Secured 4. Robust 5. Architecture neutral 6. Portable 7. Dynamic 8. Interpreted 9. High Performance 10. Multithreaded 11. Distributed Java code can be run on multiple platforms e.g.Windows,Linux,Sun Solaris,Mac/OS etc. Java code is compiled by the compiler and converted into bytecode.This bytecode is a platform independent code because it can be run on multiple platforms i.e. Write Once and Run Anywhere(WORA). Object Oriented concepts : Basic concepts of OOPs are: 1. 2. 3. 4. 5. Object Class Inheritance Polymorphism Abstraction
6. Encapsulation Object: Object means a real word entity that has state and behavior such as pen, chair, table etc. Basically it is an instance of a class. Class: Collection of objects is called class. It is a logical entity. Basically class is a collection of attributes and behavior that works as a template for creating similar type of objects. Inheritance: 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. Polymorphism: When one task is performed by different ways i.e. known as polymorphism. For example: + operator show polymorphic behavior in java , when it is placed between two numbers its add them and when we place + operator between two string it concatenate them. In java, we use method overloading and method overriding to achieve polymorphism. Abstraction: Hiding internal details and showing functionality is known as abstraction. For example: phone call, we don't know the internal processing .In java, we use abstract class and interface to achieve abstraction. Encapsulation: Binding (or wrapping) code and data together into a single unit is known as encapsulation. For example: capsule, it is wrapped with different medicines. A java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here. Java Naming Convention: it is not forced to follow. So, it is known as convention not rule. Name Convention class name should start with uppercase letter and be a noun e.g. String, Color, Button, System, Thread etc. If name is combined with two words, second word will start with uppercase letter always e.g. HelloWorld. interface name should start with uppercase letter and be an adjective e.g. Runnable, Remote, ActionListener etc. If name is combined with two words, second word will start with uppercase letter always e.g. HelloWorld. method name should start with lowercase letter and be a verb e.g. actionPerformed(), main(), print(), println() etc. If name is combined with two words, second word will start with uppercase letter always e.g. actionPerformed(), firstName, ActionEvent, ActionListener etc.It
is called camel casing. variable name should start with lowercase letter e.g. firstName, orderNumber etc. package name should be in lowercase letter e.g. java, lang, sql, util etc. constants name should be in uppercase MAX_PRIORITY etc. letter. e.g. RED, YELLOW, Example of class and object creation:In this example, we have created a Student class that have two data members id and name. We are creating the object of the Student class by new keyword and printing the objects value. class Student1{ int id;//data member (also instance variable) String name;//data member(also instance variable) public static void main(String args[]){ Student1 s1=new Student1();//creating an object of Student System.out.println(s1.id); System.out.println(s1.name); } } Instance variable: A variable that is created inside the class but outside the method, is known as instance variable.Instance variable doesn't get memory at compile time.It gets memory at runtime when object(instance) is created.That is why, it is known as instance variable. Example of Object and class that maintains the records of students: In this example, we are creating the two objects of Student class and initializing the value to these objects by invoking the insertRecord method on it. Here, we are displaying the state (data) of the objects by invoking the displayInformation method. class Student2{ int rollno; String name; void insertRecord(int r, String n){ //method rollno=r; name=n; }
void displayInformation(){System.out.println(rollno+" "+name);}//method public static void main(String args[]){ Student2 s1=new Student2(); Student2 s2=new Student2(); s1.insertRecord(111,"Karan"); s2.insertRecord(222,"Aryan"); s1.displayInformation(); s2.displayInformation(); } } Test it Now 111 Karan 222 Aryan As you see in the above figure, object gets the memory in Heap area and reference variable refers to the object allocated in the Heap memory area. Here, s1 and s2 both are reference variables that refer to the objects allocated in memory.

Lecture Notes