







































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A comprehensive overview of java programming fundamentals, covering key concepts such as variables, keywords, loops, methods, classes, inheritance, polymorphism, exception handling, threading, and collections. It delves into the intricacies of java syntax, data types, control flow, object-oriented programming principles, and essential libraries like math, string, and collections. Suitable for beginners and those seeking a solid foundation in java programming.
Typology: Lecture notes
1 / 79
This page cannot be seen from the preview
Don't miss anything!








































































Java is a programming language and a platform. Java is a high level, robust, object-oriented and secure programming language. Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. Platform : Any hardware or software environment in which a program runs, is known as a platform. Since Java has a runtime environment (JRE) and API, it is called a platform.
Simple Java is very easy to learn, and its syntax is simple, clean and easy to understand. According to Sun Microsystem, Java language is a simple programming language because: o Java syntax is based on C++ (so easier for programmers to learn it after C++).
o Java has removed many complicated and rarely-used features, for example, explicit pointers, operator overloading, etc. o There is no need to remove unreferenced objects because there is an Automatic Garbage Collection in Java. Object-oriented Java is an object-oriented programming language. Everything in Java is an object. Object-oriented means we organize our software as a combination of different types of objects that incorporate both data and behaviour. Platform Independent Java is platform independent because it is different from other languages like C, C++, etc. which are compiled into platform specific machines while Java is a write once, run anywhere language. A platform is the hardware or software environment in which a program runs. Secured Java is best known for its security. With Java, we can develop virus-free systems. Java is secured because: o No explicit pointer o Java Programs run inside a virtual machine sandbox Robust The English mining of Robust is strong. Java is robust because:
o class keyword is used to declare a class in Java. o public keyword is an access modifier that represents visibility. It means it is visible to all. o static is a keyword. If we declare any method as static, it is known as the static method. The core advantage of the static method is that there is no need to create an object to invoke the static method. The main() method is executed by the JVM, so it doesn't require creating an object to invoke the main() method. So, it saves memory. o void is the return type of the method. It means it doesn't return any value. o main represents the starting point of the program. o String[] args or String args[] is used for command line argument. o System.out.println() is used to print statement. Here, System is a class, out is an object of the PrintStream class, println() is a method of the PrintStream class.
At runtime, the following steps are performed:
Q) Can you save a Java source file by another name than the class name? Yes, if the class is not public. It is explained in the figure given below: Q) Can you have multiple classes in a java source file? Yes, like the figure given below illustrates:
JVM (Java Virtual Machine) is an abstract machine. It is called a virtual machine because it doesn't physically exist. It is a specification that provides a runtime environment
Data types specify the different sizes and values that can be stored in the variable. There are two types of data types in Java:
Unicode is a universal international standard character encoding that is capable of representing most of the world's written languages. Why java uses Unicode System? Before Unicode, there were many language standards: o ASCII (American Standard Code for Information Interchange) for the United States. o ISO 8859- 1 for Western European Language. o KOI- 8 for Russian. o GB18030 and BIG- 5 for Chinese, and so on. Problem This caused two problems:
Solution To solve these problems, a new language standard was developed i.e. Unicode System. In unicode, character holds 2 byte, so java also uses 2 byte for characters. lowest value: \u highest value: \uFFFF Operators in Java Operator in Java is a symbol that is used to perform operations. For example: +, - , *, / etc. There are many types of operators in Java which are given below: o Unary Operator, o Arithmetic Operator, o Shift Operator, o Relational Operator, o Bitwise Operator, o Logical Operator, o Ternary Operator and o Assignment Operator.
Java keywords are also known as reserved words. Keywords are particular words that act as a key to a code. These are predefined words by Java so they cannot be used as a variable or object name or class name.
statements that create a decision tree where the program may enter in the block of code where the condition is true.
4. Nested if-statement In nested if-statements, the if statement can contain a if or if-else statement inside another if or else-if statement. Switch Statement: In Java, Switch statements are similar to if-else-if statements. The switch statement contains multiple blocks of code called cases and a single case is executed based on the variable which is being switched. Loop Statements In programming, sometimes we need to execute the block of code repeatedly while some condition evaluates to true. However, loop statements are used to execute the set of instructions in a repeated order. 1. for loop 2. while loop 3. do-while loop Java for loop In Java, for loop is similar to C and C++. It enables us to initialize the loop variable, check the condition, and increment/decrement in a single line of code. We use the for loop only when we exactly know the number of times, we want to execute the block of code. Java for-each loop Java provides an enhanced for loop to traverse the data structures like array or collection. In the for-each loop, we don't need to update the loop variable. The syntax to use the for-each loop in java is given below. for (data_type var : array_name/collection_name){ //statements
Java while loop The while loop is also used to iterate over the number of statements multiple times. However, if we don't know the number of iterations in advance, it is recommended to use a while loop. Java do-while loop The do-while loop checks the condition at the end of the loop after executing the loop statements. When the number of iteration is not known and we have to execute the loop at least once, we can use do-while loop. It is also known as the exit-controlled loop since the condition is not checked in advance. The syntax of the do-while loop is given below. do { //statements } while (condition); Jump Statements Jump statements are used to transfer the control of the program to the specific statements. In other words, jump statements transfer the execution control to the other part of the program. Java break statement As the name suggests, the break statement is used to break the current flow of the program and transfer the control to the next statement outside a loop or switch statement. Java continue statement Unlike break statement, the continue statement doesn't break the loop, whereas, it skips the specific part of the loop and jumps to the next iteration of the loop immediately.
Coupling Coupling refers to the knowledge or information or dependency of another class. It arises when classes are aware of each other. Cohesion Cohesion refers to the level of a component which performs a single well- defined task. A single well-defined task is done by a highly cohesive method. Association Association represents the relationship between the objects. Here, one object can be associated with one object or many objects. Aggregation Aggregation is a way to achieve Association. Aggregation represents the relationship where one object contains other objects as a part of its state. Composition The composition is also a way to achieve Association. The composition represents the relationship where one object contains other objects as a part of its state.
In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling constructor, memory for the object is allocated in the memory. Constructor Overloading in Java Constructor overloading in Java is a technique of having more than one constructor with different parameter lists.
Singleton Pattern says that just "define a class that has only one instance and provides a global point of access to it". In other words, a class must ensure that only single instance should be created and single object can be used by all other classes. There are two forms of singleton design pattern o Early Instantiation: creation of instance at load time. o Lazy Instantiation: creation of instance when required. Advantage of Singleton design pattern o Saves memory because object is not created at each request. Only single instance is reused again and again.
Java allows us to declare a constructor as private. We can declare a constructor private by using the private access specifier. Note that if a constructor is declared private, we are not able to create an object of the class. Instead, we can use this private constructor in Singleton Design Pattern. Rules for Private Constructor The following rules keep in mind while dealing with private constructors. o It does not allow a class to be sub-classed. o It does not allow to create an object outside the class. o If a class has a private constructor and when we try to extend the class, a compile-time error occurs. o We cannot access a private constructor from any other class. o If all the constant methods are there in our class, we can use a private constructor.
classes. The static keyword belongs to the class than an instance of the class. 1) Java static variable If you declare any variable as static, it is known as a static variable. o The static variable can be used to refer to the common property of all objects (which is not unique for each object), for example, the company name of employees, college name of students, etc. o The static variable gets memory only once in the class area at the time of class loading. 2) Java static method If you apply static keyword with any method, it is known as static method. o A static method belongs to the class rather than the object of a class. o A static method can be invoked without the need for creating an instance of a class. o A static method can access static data member and can change the value of it. Q) Why is the Java main method static? Ans) It is because the object is not required to call a static method. If it were a non-static method, JVM creates an object first then call main() method that will lead the problem of extra memory allocation. 3) Java static block o Is used to initialize the static data member. o It is executed before the main method at the time of classloading.
Q) Can we execute a program without main() method? Ans) No, one of the ways was the static block, but it was possible till JDK 1.6. Since JDK 1.7, it is not possible to execute a Java class without the main method.
There can be a lot of usage of Java this keyword. In Java, this is a reference variable that refers to the current object. Java Inheritance
Inheritance in Java is a mechanism in which one object acquires all the properties and behaviours of a parent object. Inheritance represents the IS-A relationship which is also known as a parent-child relationship. Why use inheritance in java o For Method Overriding (so runtime polymorphism can be achieved).
In java, method overloading is not possible by changing the return type of the method only because of ambiguity.
Yes, by method overloading. You can have any number of main methods in a class by method overloading. But JVM calls main() method which receives string array as arguments only. Java Polymorphism
If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java. In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as method overriding.
The covariant return type specifies that the return type may vary in the same direction as the subclass.
The super keyword in Java is a reference variable which is used to refer immediate parent class object.
Instance Initializer block is used to initialize the instance data member. It run each time when object of the class is created.
The final keyword in java is used to restrict the user. The java final keyword can be used in many context. Final can be: