Introduction To Java

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

Introduction to Java

Java is one of most popular high level ‘Object Oriented’ programming language and has been used
widely to create various computer applications.

Applications of java

 Database applications

 Desktop applications

 Mobile applications

 Web based applications

 Games etc.

What is JVM?

 JVM stands for Java Virtual Machine which is basically a java interpreter which translates the
byte code into machine code and then executes it.

 The advantage of JVM is that once a java program is compiled into byte code. It can be run on
any platform.

What is byte code?

 Byte code is an highly optimized intermediate code produced when a java program is compiled
by java compiler.

 Byte code is also called java class file which is run by java interpreter called JVM.

What is Java IDE?

 Java Integrated Development Environment is software development kit equipped with

 Text (code) editor

 Compiler

 Debugger

 Form designer etc.

 It helps in writing code, compiling and executing java programs easily.

What is NetBeans?

Java NetBeans IDE is open source IDE used for writing java code, compiling and executing java programs
conveniently.

What is comment? How it can be written in Java program?

 Comments are used in code by programmers to document their programs –

 To provide explanatory notes to other people who read the code.

 This is especially useful where large programs are written by one programmer and maintained
by other programmers.

You can write comments in a Java program in the following two ways:

 Beginning a comment line with two consecutive forward slashes (//)


 Writing the comment between the symbols /* and */

Variables

 Variable is a placeholder for data that can change its value during program execution.

 Technically, a variable is the name for a storage location in the computer’s internal memory and
the value of the variable is the contents at that location.

Data types

 Data type determines the type of data to be stored in a variable.

 Data type tells the compiler, how much memory to reserve when storing a variable of that type.

Declaring variable

<Data type> <variable name>;

int x;
float degree;
int a,b,c;
char ac_no;

Rules for naming variable

 Variable names can begin with either an alphabetic character, an underscore (_), or a dollar sign
($).

 Variable names must be one word. Spaces are not allowed in variable names. Underscores are
allowed.

 There are some reserved words in Java that cannot be used as variable names, for example – int

 Java is a case-sensitive language. Variable names written in capital letters differ from variable
names with the same spelling but written in small letters.

How to take input from user in java?

To take user input we use the prebuilt Scanner class. This class is available in the java.util package.

 First we need to import Scanner class in our program as:


Import java.util.Scanner

 Then we create object of Scanner class (we pass System.in as argument in its constructor
because we will take input from console) as:
Scanner user_input = new Scanner(System.in)
 Then we invoke next() method of Scanner class that returns input taken from input stream as
String object as:
String name = user_input.next()

Note: to read numeric data input, we use static method parseInt() of Integer class that takes String as
parameter and returns its equivalent as given below:

String s = user_input,next();
int num = Integer.parseInt(s);

Working with variables

Java program to Input and output a value

Java program to input and output a number


Java program to add two numbers

Operators

Operators are special symbols in programming language and perform certain specific operations.

Control flow

A program is considered as finite set of instructions that are executed in a sequence. But sometimes the
program may require executing some instructions conditionally or repeatedly. In such situations java
provides:

 Selection structures

 Repetition structures

Selection structure

Selection in program refers to Conditionals which means performing operations (sequence of


instructions) depending on True or False value of given conditions. Structure of Conditional in a program
can be as follow:
Java provides two statements for executing block of code conditionally:

 If else statement

 Switch statement

if else statement

The if statement in Java lets us execute a block of code depending upon whether an expression
evaluates to true or false. The structure of the Java if statement is as below:

Java program to check no is even or odd


if else with logical operators

Sometimes, you may want to execute a block of code based on the evaluation of two or more
conditional expressions. For this, you can combine expressions using the logical && or || operators.

Java program to check leap year

Multiple if statements

 Multiple if refers to using more than one if statement together in a program.

 It is basically used to execute more than two block of statements on the evaluation of different
conditions.

Java program to display appropriate grade of a student


Switch statement

 The switch statement is used to execute a block of code matching one value out of many
possible values.

 It is basically used to implement choices from which user can select anyone.

Java program to create calculator using switch case


Repetition Structure

 Repetition in program refers to performing operations (Set of steps) repeatedly for a given
number of times (till the given condition is true).

 Repetition is also known as Iteration or Loop. Repetitions in program can be as follows:

Java provides three statements for looping:

 while

 do..while

 for

while statement

 it is basically used when no of repetitions are not confirmed.

 It evaluates the test before executing the body of a loop.

Syntax:

Java program to display “Welcome” 10 times


Java program to display Fibonacci series

do..while statement

 it is basically used when no of repetitions are not confirmed.

 It evaluates the test after executing the body of a loop.

 It executes the statements at least once even when given condition is not true.

Syntax:
Java program to display multiples of 5 between 50 and 100

Difference between while and do while loop

for statement

 it is one of the most widely and commonly used loop.

 It is basically used when no of repetitions are confirmed.

Syntax:

Example:

Java program to display factorial of a number


Java program to check prime number

Array

 Arrays are variables that can hold more than one value of the same type, name and size.

 It occupies contiguous memory space when declared and each space is called Array Elements
which can store individual value.

 Each Array Element is uniquely identified by a number called Array Index which begins from 0.
 The [] brackets after the data type tells the Java compiler that variable is an array that can hold
more than one value.

Syntax:

Data type[] array name =  new data type[size];

Example:

Java program to input and output in array

Java program to find largest among ten numbers using array


User defined Function

 A method or function in Java is a block of statements grouped together to perform a specific


task.

 A method has a name, a return type, an optional list of parameters, and a body.

 Return statement returns a value back to calling method and it is optional.

 Parameters placed within brackets are like normal variables that allow data to be passed into
the method.

 A method may return only one value at a time.

 A method is called/invoked from another method. When a method is called, control is


transferred from the calling method to the called method and the statements inside the called
method’s are executed.

Structure of java method


Example

Object oriented programming

 Java is an Object Oriented Programming language.

 In OOP language program is collection of objects that interact with other object to solve a
problem.

What is class in java?

 Class is physical or logical entity that has certain attributes, behavior.

 It is like a template which defines how information will be managed.

 To use attributes of a class its object must be declared.

What is object in java?

 Object is instance of a class in java.

 To use attributes and invoke method of class must be associated with its corresponding object.

 From a class multiple objects can be declared and all objects can have the same type of data
members.

Designing a class

Syntax:

Creating Class Object

<class name> <object name> = new <class name()>;

Example
Constructor

 Constructor is a special method which is used to initialize the data members of the class.

 The constructor has the same name as that of class.

 The constructor has no return type and may or may not have parameter list.

 It is invoked automatically whenever new object of class is created.

Example:
Access Modifier

 Access Modifiers in java are keywords that specify the accessibility scope of data members of
class.

 Java offers following Access Modifiers:

 private

 public

private access modifier

 Members defied as private of a class cannot be accessed outside of the class.

 Data member should be kept as private to avoid vulnerable to security issues.

public access modifier

 Members defined as public of a class cab be accessed outside of the class.

 Member methods are generally kept as public to make them available to access.

 By default all the members of a class are public.

String manipulation

You might also like