The document provides an overview of the basics of Java programming. It covers the evolution of programming languages, popular languages in 2018, the history of Java, Java modules like J2SE, J2EE and J2ME. It also discusses why Java is chosen, applications of Java, and key concepts like JDK, JRE, JVM. The document then explains how to create Java code, variables and data types, arrays, Strings, and the Object class in Java.
5. Java – History & Introduction
• James Gosling and team at Sun Microsystems , 1995
• Oak - The predecessor of Java
• Created for consumer electronics
• HotJava
• The first Java-enabled Web browser
• JDK Evolutions
• Java is “C++ -- ++”
6. 3 Modules of Java
• J2SE : Java Standard Edition
• 1 –tier or 1 layer
• VB.NET, D2K, PB, Fox Pro – Platform dependent (alternatives)
• J2EE : Java Enterprise Edition
• J2ME : Java Micro Edition
10. Compile & Run Java Program
• How to Compile?
• How to Run?
Hello.java Hello.class
Just In Time
Compiler
Hello.class
JVM OS
11. JDK & JRE
• Java Development Kit contains
tools needed to develop Java
Programs.
• Javadoc
• Javac.exe
• Java.exe
• Applet viewer etc.
• https://docs.oracle.com/javase/
8/docs/api/
• Java Runtime Environment
• Used to run Java program
• It contains JVM and Java Package
classes – Java Library
• JVM interprets the bytecode into
machine code depending upon
the underlying operating system
and hardware combination.
12.
13. First Java Program : Welcome.java
Use Pascal Notation
to declare a class
15. Questions…
• What are features of Java?
• What is Bytecode?
• Is Java Platform independent? Justify your answer.
• Is JVM Platform independent? Justify your answer.
• Is JVM Hardware or Software?
• Explain JDK, JRE and JVM.
• Explain public static void main(String args[]).
17. Outline
• How do we create code?
• Variables and data types
• Arrays & String Handling
• The Object class
18.
19. A Car Represented in Code
• Attributes: Describing the Car class
• License Plate number
• Average Miles per gallon
• Paint color
• Operations: Interacting with Car class
• Check the tail lights
• Change the paint
20. Three Independent Cars
CAR A
• License plate:
“GJ1B21890
• Average MPG:
25.5
• Paint Color: Blue
CAR B
• License plate:
“GJ02C2170
• Average MPG:
20.5
• Paint Color:
Black
CAR C
• License plate:
“GJ03D5710
• Average MPG:
23.5
• Paint Color:
White
21. Program to introduce variables
• Variable is name of reserved area allocated in memory.
• It is a name of memory location.
• Define a Car class with variable
29. Default values of Primitive Data Types
Data Type Default Value Default size
boolean false 1 bit
char 'u0000' 2 byte
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte
31. Arrays
• Set of homogeneous elements
• There are two types of array.
• Single Dimensional Array
• Multidimensional Array
• Syntax to Declare single dimensional Array in java
• dataType[] arr; (or)
• dataType []arr; (or)
• dataType arr[];
32. Array Declaration- One Dimensional
1. int a[]=new int[3];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=30;
2. int a[]={10,20,30};//declaration, instantiation and initialization
34. Which of the following statements are valid?
int[][] r = new int[2];
int[] x = new int[];
int[][] y = new int[3][];
int[][] z = {{1, 2}};
int[][] m = {{1, 2}, {2, 3}};
int[][] n = {{1, 2}, {2, 3}, };
35. Ragged Arrays or Jagged Arrays
• Each row in a two-dimensional array is itself an array. Thus, the rows
can have different lengths. (varying no of columns in each row)
• An array of this kind is known as a ragged array.
36. Ragged Arrays or Jagged Arrays
• If you If you don’t know the values in a ragged array in advance, but
do know the sizes—say, the same as before—you can create a ragged
array using the following syntax:
• triangleArray[0].length = 5
• triangleArray[1].length = 4
• triangleArray[2].length = 3
• triangleArray[3].length = 2
• triangleArray[4].length = 1
37. What is the output of the following code?
Output
38. The Arrays class
Explore following methods of Array class
• Arrays.toString()
• Arrays.deepToString()
• Arrays.sort()
• Arrays.fill()
40. The String Type
• A string is a sequence of characters.
• The java String is immutable.
• Whenever we change any string,
a new instance is created.
• For mutable string,
you can use
StringBuffer & StringBuilder classes.
41. Two ways to create String object:
String Literal
• String s1="Welcome";
• String s2="Welcome";//will not create new instance
By new
keyword
• String s=new String("Welcome");
//creates two objects and one reference variable
42. Java uses the concept of string literal, that is
why string objects are immutable in java.
46. StringBuffer class
class creates strings of flexible length that can be
modified in terms of both length and content.
• Available in java.lang package
• Thread-safe
Output
16
20
21
Reserves room for 16 characters
without reallocation
Sets initial contents of StringBuffer and
allocates room for 16 characters
Explicitly sets size of buffer
50. The StringBuilder class
• StringBuilder is a relatively recent addition to Java’s string handling
capabilities.
• StringBuilder is similar to StringBuffer except for one important
difference: it is not synchronized, which means that it is not thread-
safe.
• The advantage of StringBuilder is faster performance.
• However, in cases in which a mutable string will be accessed by
multiple threads, and no external synchronization is employed, you
must use StringBuffer rather than StringBuilder.
51. StringTokenizer class
• The processing of text often consists of
parsing a formatted input string.
• Parsing is the division of text into a set of
discrete parts, or tokens, which in a certain
sequence can convey a semantic meaning.
• The StringTokenizer class provides the first
step in this parsing process, often called the
lexer (lexical analyzer) or scanner.
• StringTokenizer implements the
Enumeration interface. Therefore, given an
input string, you can enumerate the
individual tokens contained in it using
StringTokenizer.
• To use StringTokenizer, you specify an input
string and a string that contains delimiters.
• Delimiters are characters that separate
tokens.
Delimiter: space (“ “)
53. The Object Class
• To use StringTokenizer, you specify an input string and a string that
contains delimiters.
• Delimiters are characters that separate tokens.
54. The Object Class
• There is one special class, Object, defined by Java. All other classes
are subclasses of Object.
• That is, Object is a superclass of all other classes. This means that a
reference variable of type Object can refer to an object of any other
class. Also, since arrays are implemented as classes, a variable of type
Object can also refer to any array.
• Object defines the following methods, which means that they are
available in every object.