




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























































































Day1: Java Introduction 1
Basics of any other programming language
Java is one of the most popular programming languages in the world, it is an object- oriented programming language. The Java platform provides a complete environment for application development on desktops and servers and deployment in embedded environments.
Java was developed by James Gosling in 1995, at Sun Microsystems which was later acquired by Oracle in 2010. It was originally called OAK after an OAK tree that stood outside Gosling’s Office, later it was renamed “Green” and was finally renamed to “Java” inspired by Java Coffee, that’s why its logo looks like a cup of coffee.
We have 3 editions of Java for building a different kinds of applications
This is the core Java platform, it is a specification, which contains the core libraries to develop standalone, networking, database, GUI, multithreaded types of applications. In addition to the core API, the Java SE platform consists of the virtual machine,
Day1: Java Introduction 2
development tools, deployment technologies and other class libraries and toolkit commonly used in Java application
The Java EE platform provides an API and runtime environment for developing and running large-scale, multi-tiered, scalable, reliable, and secure network applications.
It is a subset of Java SE, designed used for microdevices and embedded development like mobile phones, sensors, micro-controller, TV set-top boxes etc.
Features of Java
Day1: Java Introduction 4
JDK = (JRE + Development tools like java compiler, debugger, etc.) JRE = (JVM + Predefined Library classes)
We Problem:
Downloading and Installing the JDK software: We can download the JDK software from the Official website of Oracle. https://www.oracle.com/java/technologies/downloads
After downloading we need to install this software as normal/ordinary software on our computer.
Day1: Java Introduction 5
We can develop our java application inside a normal text editor like a notepad and can execute the application on the command prompt or terminal also. but in real-time to develop a java application we need to use IDE software (Integrated Development Environment). Inside an IDE software, all the application development environments like editor, terminal, code intelligence, etc. are there in one place, with the help of an IDE software our application development speed and productivity will be improved.
There are many IDE software are there like: IntelliJ IDEA Eclipse NetBeans
Downloading and Installing the IDE software (IntelliJ IDEA)
We can download the IntelliJ IDEA community edition from the following website: https://www.jetbrains.com/idea/
From here we can download the community edition of the IntelliJ IDEA (for Windows OS, we can download either a .exe file and install it as a normal software application or we can download a zip file and just unzip this file to make use of IntelliJ IDEA software).
To develop the Java application step by step in IntelliJ IDEA, you can refer to the following link: https://www.jetbrains.com/help/idea/creating-and-running-your-first-java-application.html
Day1: Java Introduction 7
If we are in the Window Operating-system, this JVM will convert our java byte-code to the window Operating-system understandable native code, and if we are in the Linux environment, then this JVM will convert our java byte-code to the Linux OS understandable native code. with this architecture only, our java applications are portable or platform-independent. We can write a java program on a Window machine and can execute it in a Linux or macOS or any other OS that have JRE.
The smallest building block in java application is function/method. A method is a block of code that performs a well-defined task. example: a method for adding 2 numbers, a method for validating user input, a method for calculating interest, etc. the basic syntax of a method in Java application is:
returnType methodName(){ --instructions-- }
Some method returns some value, whereas some method does not return any value. the method which does not return any value we apply void as a return type. void is a reserved keyword in java, the method name should be proper and descriptive. Example:
void calculateInterest(){ }
We can pass some parameters to this method also, we use parameters to pass the value to our method. for example, amount, rate of interest, duration, etc. and inside the pair of parenthesis { }, we write the actual implementation java code.
Day1: Java Introduction 8
Every java program should have at least one method. and that method is called the main method. The main method is an entry point of our java application.
Whenever we execute a java program, the main function gets called and the code inside this main method gets executed.
These methods don't exist as their own, they always belong to a class. A class is like a container, of one or more methods.
We use the class to organize our code in the java application. Every java program should have at least one class, which contains the main method.
I Problem
Main.java
package com.masai; public class Main{ public static void main(String[] args){ System.out.println("Hello World"); } }
Day1: Java Introduction 10
Note: All java files should have a .java extension. and every statement in the java application should be terminated with a semicolon ;
The static is a keyword, we will talk about this keyword later, for now, just remember the main method in our program should always be static. and the return type of this method is void, which means this method is not going to return any value.
In the main method, we have one parameter, we can use this parameter to pass values to our program. we will talk about this parameter in our upcoming session.
System.out.println("Hello World");
Here System is a predefined java class, which belongs to java.lang package. inside this class, we have various members, out is a member (field) who belongs to this System class. the type of this out field is the PrintStream class. this PrintStream is another predefined class in java. the println method belongs to this PrintStream class.
So here we are calling or executing the println method inside our main method.
Inside the parenthesis of this println method, we can pass any value, which we want to print on the terminal or console.
Here “Hello World” is textual data, in java whenever we deal with textual data we should always surround them with double-quotes. which is known as a string. In java, a string is a sequence of characters.
Day1: Java Introduction 11
You Problem:
Write a java application that will print Your Name and Address in the following pattern:
My Name is: Your Name My Address is: Your Address
Day2: Java Language Fundamentals 2
int age = 25; int height; //default to be 0 height=67; //0, 1, ... here int : data type age : variable [identifier] = : operator (assingment) 25 : value/constant [literal] ; : terminator
Note: To provide identifiers in java programming, we have to use the following rules and regulations:-
Identifiers should not be started with any number, identifiers may be started with an alphabet, '_ ' symbol, '$' symbol, but, the subsequent symbols may be a number, an alphabet, '_ ' symbol, '$' symbol. //camelCase (recommendation, convention) = thisIsMe ⇒ variable names, Method names //PascalCase = ThisIsMe⇒ classes, interfaces, abstract classes Identifiers are not allowing spaces in the middle. ex: int employeeNumber = 111; // valid String #address = "Pune"; // Invalid String employee-Address="Hyderabad"; // Invalid getInputStream(); // valid get Input Stream(); // Invalid
In java applications, it is suggestible to provide identifiers with a particular meaning.
Day2: Java Language Fundamentals 3
byte, short, int, long :- 10, 20, 30,.... char :- 'A','B',.....
Keywords in Java convey a special meaning to the compiler therefore, these cannot be used as identifiers. Among the list of keywords list mentioned below the keywords goto and const are currently not in use. They are reserved words (for future use).. ex:-
abstract continue for new switch assert default goto package synchronized boolean do if private this break double implements protected throw byte else import public throws case enum instanceof return transient catch extends int short try char final long strictfp volatile const float native super while
4. Operators:
Day2: Java Language Fundamentals 5
public static void main(String[] args){ int a=10; System.out.println(a); // System.out.println(a++);//10 //post-increment => (print, increase) System.out.println(++a);// System.out.println(a--);// System.out.println(--a);// System.out.println(a);// } }
Output:- 10 10 12 12 10 10
Example 2:
package com.masai; public class Main{ public static void main(String[] args){ int a=5; System.out.println(++a - ++a); } }
Output: -
Example 3 :
package com.masai; public class Main{ public static void main(String[] args){ int a=5; System.out.println((--a+--a)(++a-a--)+(--a+a--)(++a+a++)); } }
Day2: Java Language Fundamentals 6
Output : 16 explanation : (4 + 3) * (4 - 4) + (2 + 2) * (2 + 2)
Example: Boolean Logical Operator This operator can also be applied the boolean value as well as an integer.
T T T T F T F F T T F T F T T F F F F F
0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 1 1 1 0
Example 1:
package com.masai; public class Main{ public static void main(String[] args{ boolean b1=true; boolean b2=false; System.out.println(b1&b1);//true System.out.println(b1&b2);//false System.out.println(b2&b1);//false System.out.println(b2&b2);//false System.out.println(b1|b1);//true System.out.println(b1|b2);//true System.out.println(b2|b1);//true System.out.println(b2|b2);//false System.out.println(b1^b1);//false System.out.println(b1^b2);//true System.out.println(b2^b1);//true System.out.println(b2^b2);//false } }
package com.masai; public class Main{
Day2: Java Language Fundamentals 8
package com.masai; public class Main{ public static void main(String[] args { int a=10; int b=10; if( (a++ == 10) | (b++ == 10) ) { System.out.println(a+" "+b);//OUTPUT: 11 11 } int c=10; int d=10; if( (c++ == 10) || (d++ == 10) ) { System.out.println(c+" "+d);//OUTPUT: 11 10 } } }
& vs && In the case of Logical-AND operator, if the first operand value is false then it is not required to check second operand value, directly, we can predict the result of overall expression is false. In the case of '&' operator, even first operand value is false , still, JVM evaluates second operand value then only JVM will get the result of overall expression is false, here evaluating second operand value is unnecessary, it will increase execution time and it will reduce application performance. In the case of '&&' operator, if the first operand value is false then JVM will get the overall expression result is false with out evaluating second operand value, here JVM is no evaluating second operand expression unnecessarily, it will reduce execution time and it will improve application performance. Note: If the first operand value is true then it is mandatory for JVM to evaluate second operand value in order to get overall expression result.
package com.masai; public class Main{ public static void main(String[] args) { int a=10; int b=10; if( (a++ != 10) & (b++ != 10) ) { } System.out.println(a+" "+b);//OUTPUT: 11 11
int c=10; int d=10;
Day2: Java Language Fundamentals 9
if( (c++ != 10) && (d++ != 10) ) { } System.out.println(c+" "+d);//OUTPUT: 11 10 } }
Data Types:
Java is strictly a typed programming language, where in java applications before representing data first we have to confirm which type of data we representing. In this context, to represent type of data we have to use "data types".
In java applications , data types are able to provide the following advantages.
Reason: 'byte' data type is providing a particular range for its variables like -128 to 127, in this range only we have to assign values to byte variables.
To prepare java applications, JAVA has provided the following data types.
A. Primitive Data types :