Docsity

Complete Java Beginner Friendly Notes||Easy to understand||Guide to Master Java, Study notes of English

Toggle sidebar
bg1
Day1: Java Introduction 1
Day1: Java Introduction
Prerequisites:
Basics of any other programming language
Introduction of Java
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.
Java Edition
We have 3 editions of Java for building a different kinds of applications
1. Java Standard Edition (JSE)
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,
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Complete Java Beginner Friendly Notes||Easy to understand||Guide to Master Java and more Study notes English in PDF only on Docsity!

Day1: Java Introduction 1

Day1: Java Introduction

Prerequisites:

Basics of any other programming language

Introduction of Java

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.

Java Edition

We have 3 editions of Java for building a different kinds of applications

1. Java Standard Edition (JSE)

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

2. Java Enterprise Edition (JEE)

The Java EE platform provides an API and runtime environment for developing and running large-scale, multi-tiered, scalable, reliable, and secure network applications.

3.Java Micro Edition (JME)

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.

Note:- JSE is the basic foundation of the remaining 2 other editions.

The latest version of Java is Java 17 which was released on 14-

Sep 2021

Java has close to 10 million developers worldwide, currently,

about 3 billion mobile phones run java, Now a day java is

everywhere, which means more opportunities for us to get hired

as a professional programmers.

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.

Select Java Editor:

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.

Basics of a Java Application

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

Our first java program:

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:

This is My First Java Application

My Name is: Your Name My Address is: Your Address

Day2: Java Language Fundamentals 2

  1. Identifiers: The identifier is a name assigned to the programming elements like variables, methods, classes, abstract classes, interfaces etc. ex:

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.

  1. Literals Literal is a constant/value assigned to the variable. To prepare java programs, JAVA has provided the following set of literal:
  2. Integer/Integral Literals:

Day2: Java Language Fundamentals 3

byte, short, int, long :- 10, 20, 30,.... char :- 'A','B',.....

  1. Floating Point Literals: float : 10.22f, 23.345f,..... double :- 11.123, 456.345,....
  2. Boolean Literals: boolean:- true, false
  3. String Literals: String :- "welcome", "Hello",...... 3. Keywords/ Reserved Words

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.

A B A&B A|B A^B

T T T T F T F F T T F T F T T F F F F F

A B A&B A|B A^B

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.

  1. We are able to identify memory sizes to store data. example : int i=10;--> int will provide 4 bytes of memory to store 10 value.
  2. We are able to identify range values to the variable to assign. example: byte b=130;---> Invalid byte b=125;---> Valid

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 :

  1. Numeric Data Types a. Integral data types/ Integer Data types: byte ------> 1 bytes ----> 0 short------> 2 bytes-----> 0