Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Introduction To Java Programming

The document provides an introduction to the Java programming language. It discusses the history and development of Java, its key features such as being platform independent and object-oriented, and basic Java concepts like classes, objects, inheritance and polymorphism.

Uploaded by

mrudul mathews
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Introduction To Java Programming

The document provides an introduction to the Java programming language. It discusses the history and development of Java, its key features such as being platform independent and object-oriented, and basic Java concepts like classes, objects, inheritance and polymorphism.

Uploaded by

mrudul mathews
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 69

INtroduction to java programming

1
contents

❏ Introduction
❏ Features of Java
❏ Basics of Java
❏ Datatypes, Variables and Arrays

2
Introduction

❏ JAVA was developed by James Gosling


❏ At Sun Microsystems in the year 1991.
❏ Later acquired by Oracle Corporation.
❏ Makes writing, compiling, and debugging programming easy.
❏ Helps to create reusable code and modular programs.
❏ Class-based, object-oriented programming language.

3
Introduction

❏ write once run anywhere: Compiled Java code can run on all
platforms that support Java.
❏ Java applications are compiled to bytecode that can run on any Java
Virtual Machine.

4
Introduction
❏ Bytecode
❏ Output of a Java compiler is not executable code.
❏ Bytecode is a highly optimized set of instructions.
❏ .class file
❏ Designed to be executed by the Java run-time system, which is
called the Java Virtual Machine (JVM).
❏ JVM is an interpreter for bytecode.
❏ Create portable programs.
❏ Secure: Execution of every Java program is under the control of
the JVM, the JVM can contain the program and prevent it from
generating side effects outside of the system.
5
Introduction

6
Introduction

❏ The Java Buzzwords


❏ Architecture-neutral
❏ Simple
❏ Interpreted
❏ Secure
❏ High performance
❏ Portable
❏ Distributed
❏ Object-oriented
❏ Dynamic
❏ Robust
❏ Multithreaded

7
Features of Java
❏ Platform Independent:
❏ Bytecode can run on any platform be it Windows, Linux, macOS.
❏ If we compile a program on Windows, then we can run it on Linux
and vice versa.
❏ Each operating system has a different JVM, but the output
produced by all the OS is the same after the execution of
bytecode.

8
Features of Java
❏ Object-Oriented Programming Language:
❏ Organizing the program in the terms of collection of objects.
❏ Each of which represents an instance of the class.
❏ The four main concepts of Object-Oriented programming are:
❏ Abstraction
❏ Encapsulation
❏ Inheritance
❏ Polymorphism

9
Features of Java
❏ Simple:
❏ Does not have complex features like pointers, operator
overloading, multiple inheritances, Explicit memory allocation.
❏ Robust:
❏ Reliable
❏ Puts a lot of effort into checking errors as early as possible.
❏ Java compiler is able to detect even those errors that are not easy to
detect by another programming language.
❏ Features of java that make it robust are garbage collection,
Exception Handling, and memory allocation.

10
Features of Java
❏ Secure:
❏ Several security flaws like stack corruption or buffer overflow is
impossible to exploit in Java.
❏ Distributed:
❏ We can create distributed applications using the java programming
language.
❏ Remote Method Invocation and Enterprise Java Beans are used for
creating distributed applications in java.
❏ The java programs can be easily distributed on one or more systems
that are connected to each other through an internet connection.

11
Features of Java
❏ Multithreading:
❏ Java supports multithreading.
❏ It is a Java feature that allows concurrent execution of two or more parts
of a program for maximum utilization of CPU.
❏ Portable:
❏ Java code written on one machine can be run on another machine.
❏ Platform-independent
❏ High Performance:
❏ Reduces overhead during the runtime.
❏ At some time java uses Just In Time (JIT) compiler
❏ Compiles code on-demand basics where it only compiles those
methods that are called, making applications to execute faster.
12
Features of Java
❏ Dynamic flexibility:
❏ Java being completely object-oriented gives us the flexibility to
add classes, new methods to existing classes and even creating
new classes through sub-classes.
❏ Java even supports functions written in other languages such as C,
C++ which are referred to as native methods.

13
Features of Java
❏ Sandbox Execution:
❏ Java programs run in a separate space that allows user to execute
their applications without affecting the underlying system with
help of a bytecode verifier.
❏ Bytecode verifier also provides additional security as it’s role is to
check the code for any violation access.
❏ Write Once Run Anywhere (WORA)

14
Features of Java
❏ Power of compilation and interpretation:
❏ Most languages are designed with purpose either they are
compiled language or they are interpreted language.
❏ But java integrates arising enormous power as Java compiler
compiles the source code to bytecode and JVM executes this
bytecode to machine OS-dependent executable code.

15
Basics of Java

❏ OOPs (Object Oriented Programming System):


❏ Way of solving a complex problem by breaking them into a small
sub-problem.
❏ An object is a real-world entity.
❏ In OOPs, we create programs using class and object in a
structured manner.

16
Basics of Java

❏ Class: A class is a template or blueprint or prototype that defines


data members and methods of an object.
❏ An object is the instance of the class.
❏ We can define a class by using the class keyword.

17
Basics of Java
❏ Object: An object is a real-world entity that can be identified
distinctly.
❏ For example, a desk, a circle can be considered as objects.
❏ An object has a unique behavior, identity, and state.
❏ Data fields with their current values represent the state of an
object (also known as its properties or attributes).

18
Basics of Java

19
Basics of Java
❏ Abstraction: An abstraction is a method of hiding irrelevant
information from the user.
❏ For example, the driver only knows how to drive a car; there
is no need to know how does the car run.
❏ We can make a class abstract by using the keyword abstract.
❏ In Java, we use abstract class and interface to achieve
abstraction.

20
Basics of Java
❏ Encapsulation: An encapsulation is the process of binding data
and functions into a single unit.
❏ A class is an example of encapsulation.
❏ In Java, Java bean is a fully encapsulated class.
❏ Inheritance: Inheritance is the mechanism in which one class
acquire all the features of another class.
❏ We can achieve inheritance by using the extends keyword.
❏ It facilitates the reusability of the code.

21
Basics of Java
❏ Polymorphism: The polymorphism is the ability to appear in
many forms.
❏ In other words, single action in different ways.
❏ For example, a boy in the classroom behaves like a student, in
house behaves like a son.
❏ There are two types of polymorphism: run time polymorphism
and compile-time polymorphism.

22
Basics of Java
❏ Java programs are a collection of whitespace, identifiers, comments,
literals, operators, separators, and keywords.

❏ Whitespace:
❏ At least one whitespace character between each token.
❏ In Java, whitespace is a space, tab, or newline.

23
Basics of Java
❏ Identifiers:
❏ Used for class names, method names, and variable names.
❏ Any descriptive sequence of uppercase and lowercase letters,
numbers, or the underscore and dollar-sign characters.
❏ Must not begin with a number.
❏ Case-sensitive.
❏ Eg: AvgTemp, count, a4, $test, this_is_ok

24
Basics of Java
❏ Literals:
❏ A constant value in Java is created by using a literal representation
of it.
❏ Eg: 100, 98.6, ‘X’, “This is a test”
❏ Comments:
❏ Three types of comments defined by Java.
❏ single-line (//) and multiline (/*........*/)
❏ documentation comment: used to produce an HTML file that
documents your program. The documentation comment begins with
a /** and ends with a */.

25
Basics of Java

❏ Separators:
❏ The most commonly used separator in Java is the semicolon.
❏ ; terminate statements.

26
Basics of Java

27
Basics of Java
❏ The Java Keywords:
❏ 49 reserved keywords
❏ These keywords, combined with the syntax of the operators and
separators, form the definition of the Java language.
❏ These keywords cannot be used as names for a variable, class, or
method.
❏ keywords const and goto are reserved but not used.

28
Basics of Java

29
Basics of Java
❏ Simple Program:
import java.io.*;
public class Simple
{
public static void main (String args[ ])
{
System.out.println(“Hello Java”);
}
}

30
Datatypes, Variables and Arrays
❏ Eight simple (or elemental) types of data:
❏ byte
❏ short
❏ int
❏ long
❏ char
❏ float
❏ double
❏ boolean

31
Datatypes, Variables and Arrays
❏ These can be put in four groups:
❏ Integers: This group includes byte, short, int, and long, which are for
whole valued signed numbers.
❏ Floating-point numbers: This group includes float and double, which
represent numbers with fractional precision.
❏ Characters: This group includes char, which represents symbols in a
character set, like letters and numbers.
❏ Boolean: This group includes boolean, which is a special type for
representing true/false values.

32
Datatypes, Variables and Arrays

33
Integers
❏ Signed, positive and negative values.

34
Integers
❏ byte:
❏ The smallest integer type is byte.
❏ This is a signed 8-bit type that has a range from –128 to 127.
❏ Useful when you’re working with a stream of data from a network
or file.
❏ Keyword: byte
❏ Eg: byte b, c;

35
Integers
❏ short:
❏ Signed 16-bit type.
❏ It has a range from –32,768 to 32,767
❏ Least-used Java type
❏ Eg: short s;

short t;

36
Integers
❏ int:
❏ The most commonly used integer type.
❏ It is a signed 32-bit type that has a range from –2,147,483,648 to
2,147,483,647.
❏ Variables of type int are commonly employed to control loops and
to index arrays.
❏ Most versatile and efficient type
❏ Eg: int a = 10;

37
Integers
❏ long:
❏ Signed 64-bit type
❏ Useful for those occasions where an int type is not large enough to
hold the desired value.
❏ The range of a long is quite large.
❏ This makes it useful when big, whole numbers are needed.
❏ Eg: long distance = 16070400000000;

38
Integers

39
Floating-Point Types
❏ Floating-point numbers, also known as real numbers, are used when
evaluating expressions that require fractional precision.

40
Floating-Point Types
❏ float:
❏ Single-precision value that uses 32 bits of storage.
❏ Variables of type float are useful when you need a fractional
component, but don’t require a large degree of precision.
❏ Eg: float hightemp, lowtemp;
❏ double:
❏ Keyword: double
❏ Uses 64 bits to store a value
❏ Eg: double pi = 3.1416;

41
CHARACTERS
❏ Used to store characters.
❏ Keyword: char
❏ char is a 16-bit type.
❏ The range of a char is 0 to 65,536.
❏ Java uses Unicode to represent characters.
❏ Unicode defines a fully international character set that can represent all
of the characters found in all human languages.
❏ Eg: char c = ‘X’;
❏ char c= 88; (c = ‘X’; code for X = 88)

42
Booleans
❏ boolean for logical values
❏ Two possible values, true or false.
❏ Type returned by all relational operators, such as a < b.
❏ boolean is also the type required by the conditional expressions that
govern the control statements such as if and for.
❏ Eg: boolean b;
b = false;

43
Sample programs
public class DataType
{
public static void main(String args[])
{
int marks;
Output:
char grade;
marks = 50; Marks: 50
grade = 'B'; Grade: B
System.out.println("Marks: "+marks);
System.out.println("Grade: "+grade);
}
}

44
Sample programs
// char can be handled like integers
public class CharClass
{
public static void main(String args[]) Output:
{
char myChar1 = 'A'; myChar1: A
char myChar2 = 'B'; myChar2: B
System.out.println("myChar1: " +myChar1); The incremented
System.out.println("myChar2: " +myChar2); value of myChar2:
myChar2++; // valid increment operation C
System.out.println("The Incremented value of myChar2: "
+myChar2);

45
Sample programs
public class ShortDataType
{
public static void main(String args[])
{ Output:
short myShort = 6000;
myShort: 6000
System.out.println("myShort: " + myShort);
}
}

46
Sample programs
public class FloatDataType
{
public static void main(String args[])
{
float myFloat1,myFloat2,result;
Output:
myFloat1=1000.666f;
myFloat2=110.77f; Number1: 1000.666
result=myFloat1-myFloat2; Number2: 110.77
Number1-Number2:
System.out.println("Number1: "+myFloat1); 889.896
System.out.println("Number2: "+myFloat2);
System.out.println("Number1-Number2: "+result);
}
}

47
Sample programs
public class DoubleDataType
{
public static void main(String args[])
{
double myDouble1, myDouble2, result;
Output:
myDouble1 = 48976.8987; Number 1: 48976.8987
myDouble2 = 29513.7812d; Number 2: 29513.7812
result = myDouble1 + myDouble2; Number 1 + Number 2:
78490.6799
System.out.println("Number 1: " +myDouble1);
System.out.println("Number 2: " +myDouble2);
System.out.println("Number 1 + Number 2: " +result);
}
}

48
Sample programs
public class BooleanDataType
{
public static void main(String args[])
{
Output:
boolean myBool = true;
if(myBool == true) I am using a Boolean
data type
System.out.println("I am using a Boolean data type");
true
System.out.println(myBool);
}
}

49
LITERALS
❏ Integer Literals:
❏ Any whole number value is an integer literal.
❏ Decimal values: 1, 2, 56, 43…
❏ There are two other bases which can be used in integer literals,
octal (base eight) and hexadecimal (base 16).
❏ Octal values are denoted in Java by a leading zero; 0 to 7 range
❏ Hexadecimal constant with a leading zero-x, (0x or 0X).
❏ The range of a hexadecimal digit is 0 to 15, so A through F (or a
through f ) are substituted for 10 through 15.

50
LITERALS
❏ Possible to assign an integer literal to one of Java’s other integer
types, such as byte or long, without causing a type mismatch error.
❏ To specify a long literal, need to explicitly tell the compiler that
the literal value is of type long.
❏ By appending an upper- or lowercase L to the literal.
❏ For example, 0x7ffffffffffffffL or 9223372036854775807L is
the largest long.

51
LITERALS
❏ Floating-Point Literals:
❏ Decimal values with a fractional component.
❏ Eg: 2.039, 5.67, 3.14159
❏ To specify a float literal, you must append an F or f to the
constant.
❏ Explicitly specify a double literal by appending a D or d.
❏ The default double type consumes 64 bits of storage, while the
less-accurate float type requires only 32 bits.
❏ Eg: float f = 2.45 f;

52
LITERALS
❏ Boolean Literals:
❏ Only two logical values that a boolean value can have,
❏ true and false
❏ The values of true and false do not convert into any numerical
representation.
❏ The true literal in Java does not equal 1, nor does the false literal
equal 0.
❏ They can only be assigned to variables declared as boolean, or
used in expressions with Boolean operators.

53
LITERALS
❏ Character Literals:
❏ Characters in Java are indices into the Unicode character set.
❏ 16-bit values that can be converted into integers and manipulated
with the integer operators
❏ A literal character is represented inside a pair of single quotes.
❏ Eg: char a= ‘C’;

54
LITERALS

55
LITERALS
❏ String Literals:
❏ By enclosing a sequence of characters between a pair of double
quotes.
❏ Eg: “Hello World”, “two\nlines” , “\”This is in quotes\””
❏ One important thing to note about Java strings is that they must
begin and end on the same line.
❏ There is no line-continuation escape sequence as there is in other
languages.

56
VARIABLES
❏ Variable is the basic unit of storage in a program.
❏ A variable is defined by the combination of an identifier, a type, and an
optional initializer.
❏ All variables have a scope, which defines their visibility, and a
lifetime.
❏ Declaring a Variable:
❏ type identifier [ = value][, identifier [= value] ...] ;
❏ Eg: int a =10, b = 20, c = 30;

57
VARIABLES
❏ type : datatype/ name of a class or interface.
❏ identifier: name of variable.

58
VARIABLES
❏ Dynamic Initialization:
❏ Java allows variables to be initialized dynamically, using any
expression valid at the time the variable is declared.

59
ARRAYS
❏ One-Dimensional Arrays:
❏ Declaration: type var_name[ ];
❏ Eg: int month_days[ ];
❏ To link month_days with an actual, physical array of integers, you
must allocate one using new and assign it to month_days.
❏ new is a special operator that allocates memory.
❏ var_name = new type[size];
❏ Eg: month_days = new int[12];
❏ Now month_days will refer to an array of 12 integers

60
ARRAYS
❏ Access a specific element in the array by specifying its index
within square brackets.
❏ All array indexes start at zero
❏ month_days[1] = 28; // assigns the value 28 to the second element
of month_days
❏ System.out.println(month_days[3]); // displays the value stored at
index 3

61
ARRAYS
// Demonstrate a one-dimensional array.
class Array {
public static void main(String args[]) {
int month_days[];
month_days = new int[12];
month_days[0] = 31;
month_days[1] = 28;
month_days[2] = 31;
month_days[3] = 30;
month_days[4] = 31;
month_days[5] = 30;
month_days[6] = 31;
month_days[7] = 31;
month_days[8] = 30;
month_days[9] = 31;
month_days[10] = 30;
month_days[11] = 31;
System.out.println("April has " + month_days[3] + " days.");
}
}

62
ARRAYS
❏ Arrays can be initialized when they are declared.
❏ An array initializer is a list of comma-separated expressions
surrounded by curly braces.
❏ The commas separate the values of the array elements.
❏ The array will automatically be created large enough to hold the
number of elements you specify in the array initializer.
❏ There is no need to use new.
❏ Eg: int month_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
31 };

63
ARRAYS

❏ Scanner sc = new Scanner(System.in);


for (int i=0; i <12; i++)
{
month_days[i] = sc.nextInt();
}

64
ARRAYS
// initializing array

import java.io.*; System.out.println("\n\n***Initializing


import java.util.Scanner; Array***");
public class Test {
public static void main(String[] args) for(int i=0; i < 5; i++) {
// read input
{ arr[i] = scan.nextInt();
}
// Scanner class object to read input
Scanner scan = new Scanner(System.in); //displaying initialized values
System.out.println("Array elements
// declaring and creating array objects are:");
for (int i=0; i < 5; i++) {
int[] arr = new int[5]; System.out.print(arr[i]+"\t");
}

}
}

65
ARRAYS

❏ Multidimensional Arrays:
❏ int twoD[][] = new int[4][5];
❏ Allocates a 4 by 5 array and assigns it to twoD.
❏ Internally this matrix is implemented as an array of arrays of int.

66
ARRAYS

67
String

❏ String type is used to declare string variables.


❏ A quoted string constant can be assigned to a String variable.
❏ String is an array of characters.
❏ String str = "this is a test";
System.out.println(str);

68
THANK YOU

69

You might also like