0% found this document useful (0 votes)
12 views

Java More Details

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Java More Details

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Java

More Details
Array

2
Arrays
•  A group of variables containing values that all have
the same type
•  Arrays are fixed-length en;;es
•  In Java, arrays are objects, so they are considered
reference types
•  But the elements of an array can be either primi;ve
types or reference types

3
Arrays
•  We access the element of an array using the
following syntax
–  name[index]
–  “index” must be a nonnega;ve integer
•  “index” can be int/byte/short/char but not long
•  In Java, every array knows its own length
•  The length informa;on is maintained in a public final
int member variable called length

4
Declaring and Crea;ng Arrays
•  int c[ ] = new int [12]
–  Here, “c” is a reference to an integer array
–  “c” is now poin;ng to an array object holding 12 integers
–  Like other objects arrays are created using “new” and are
created in the heap
–  “int c[ ]” represents both the data type and the variable
name. Placing number here is a syntax error
–  int c[12]; // compiler error

5
Declaring and Crea;ng Arrays
•  int[ ] c = new int [12]
–  Here, the data type is more evident i.e. “int[ ]”
–  But does the same work as
•  int c[ ] = new int [12]
•  Is there any difference between the above two
approaches?

6
Declaring and Crea;ng Arrays
•  int c[ ], x
–  Here, ‘c’ is a reference to an integer array
–  ‘x’ is just a normal integer variable
•  int[ ] c, x;
–  Here, ‘c’ is a reference to an integer array (same as before)
–  But, now ‘x’ is also a reference to an integer array

7
Using an Array Ini;alizer
•  We can also use an array ini;alizer to create an array
–  int n[ ] = {10, 20, 30, 40, 50}
•  The length of the above array is 5
•  n[0] is ini;alized to 10, n[1] is ini;alized to 20, and so
on
•  The compiler automa;cally performs a “new”
opera;on taking the count informa;on from the list
and ini;alizes the elements properly

8
Arrays of Primi;ve Types
•  When created by “new”, all the elements are
ini;alized with default values
–  byte, short, char, int, long, float and double are ini;alized
to zero
–  boolean is ini;alized to false
•  This happens for both member arrays and local arrays

9
Arrays of Reference Types
•  String [] str = new String[3]
–  Only 3 String references are created
–  Those references are ini;alized to “null” by default
–  Need to explicitly create and assign actual String objects in
the above three posi;ons.
•  str[0] = new String(“Hello”);
•  str[1] = “World”;
•  str[2] = “I” + “ Like” + “ Java”;

10
Passing Arrays to Methods
void modifyArray(double d[ ]) {…}
double [] temperature = new double[24];
modifyArray(temperature);
•  Changes made to the elements of ‘d’ inside
“modifyArray” is visible and reflected in the
“temperature” array
•  But inside “modifyArray” if we create a new array
and assign it to ‘d’ then ‘d’ will point to the newly
created array and changing its elements will have no
effect on “temperature”
11
Passing Arrays to Methods
•  Changing the elements is visible, but changing the
array reference itself is not visible

void modifyArray(double d[ ]) {
d[0] = 1.1; // visible to the caller
}
void modifyArray(double d[ ]) {
d = new double [10];
d[0] = 1.1; // not visible to the caller
}
12
Mul;dimensional Arrays
•  Can be termed as array of arrays.
•  int b[ ][ ] = new int[3][4];
–  Length of first dimension = 3
•  b.length equals 3
–  Length of second dimension = 4
•  b[0].length equals 4
•  int[ ][ ] b = new int[3][4];
–  Here, the data type is more evident i.e. “int[ ][ ]”

13
Mul;dimensional Arrays
•  int b[ ][ ] = { { 1, 2, 3 }, { 4, 5, 6 } };
–  b.length equals 2
–  b[0].length and b[1].length equals 3
•  All these examples represent rectangular two
dimensional arrays where every row has same
number of columns
•  Java also supports jagged array where rows can have
different number of columns

14
Mul;dimensional Arrays
Example – 1 Array ‘b’
int b[ ][ ];
b = new int[2][ ];
b[0] = new int[2];
b[1] = new int[3]; Col 0 Col 1 Col 2
b[0][2] = 7; //will throw an excep;on
Row 0
Example – 2
int b[ ][ ] = { { 1, 2 }, { 3, 4, 5 } }; Row 1
b[0][2] = 8; //will throw an excep;on

In both cases
b.length equals 2
b[0][2] does not exist
b[0].length equals 2
b[1].length equals 3

15
Command Line Arguments

16
Using Command-Line Arguments
•  java MyClass arg1 arg2 … argN
–  words aner the class name are treated as command-line
arguments by Java
–  Java creates a separate String object containing each
command-line argument, places them in a String array and
supplies that array to main
–  That’s why we have to have a String array parameter
(String args[ ]) in main System.out.print(args[0]);
–  We do not need a “argc” type parameter (for parameter
coun;ng) as we can easily use “args.length” to determine
the number of parameters supplied.

17
Using Command-Line Arguments

3
Hello
java CommandLineTest Hello 2 You
2
You
18
For-Each

19
For-Each version of the for loop

20
{
int a = 5;

{ int b = 10;

cout<<a<<b<<endl; 510
}
cout<<a<<b<<endl; //error
}
Nested and Inner Classes

21
Nested Classes
•  It is possible to define a class within another classes,
such classes are known as nested classes
•  The scope of nested class is bounded by the scope of
its enclosing class. That means if class B is defined
within class A, then B doesn’t exists without A
•  The nested class has access to the members
(including private!) of the class in which it is nested
•  The enclosing class doesn’t have access to the
members of the nested class

22
A{
int a;
static B{
Sta;c Nested Classes
----------------
A ob => a
stack pointer
}
•  Two types of nested classes. ----------------
static
} –  Sta;c ----------------
–  Non-Sta;c code => main: int a
----------------
•  A sta;c nested class is one which has the sta;c
modifier applied. Because it is sta;c, it must access
the members of its enclosing class through an object
•  That is, it cannot refer to members of its enclosing
class directly. Because of this restric;on, sta;c
nested classes are seldom used

23
Inner Classes
•  The most important type of nested class is the inner
class
•  An inner class is a non-sta;c nested class
•  It has access to all of the variables and methods of its
outer class and may refer to them directly in the
same way that other non-sta;c members of the
outer class do
•  Thus, an inner class is fully within the scope of its
enclosing class

24
Inner Classes

25
Sta;c Nested Classes

26
Inner Classes

27
Inner Classes within blocks

28
Scanner

29
Scanner
•  It is one of the u;lity class located in the java.u;l
package
•  Using Scanner class, we can take inputs from the
keyboard
•  Provides methods for scanning
–  Int
–  float
–  Double
–  Line etc.

30
Scanner

31
JOp;onPane

32
StaUc

33
Sta;c Variables
•  When a member (both methods and variables) is
declared sta;c, it can be accessed before any objects
of its class are created, and without reference to any
object
•  Sta;c variable
–  Instance variables declared as sta;c are like global
variables
–  When objects of its class are declared, no copy of a sta;c
variable is made

34
Sta;c Methods & Blocks
•  Sta;c method
–  They can only call other sta;c methods
–  They must only access sta;c data
–  They cannot refer to this or super in any way
•  Sta;c block
–  Ini;alize sta;c variables.
–  Get executed exactly once, when the class is first loaded

35
Sta;c

36
Final
•  Declare a final variable, prevents its contents from
being modified
•  final variable must ini;alize when it is declared
•  It is common coding conven;on to choose all
uppercase iden;fiers for final variables
final int FILE_NEW = 1;
final int FILE_OPEN = 2;
final int FILE_SAVE = 3;
final int FILE_SAVEAS = 4;
final int FILE_QUIT = 5;
37
Unsigned right shin operator
•  The >> operator automa;cally fills the high-order bit
with its previous contents each ;me a shin occurs
•  This preserves the sign of the value
•  But if you want to shin something that doesn’t
represent a numeric value, you may not want the
sign extension
•  Java’s >>> shins zeros into the high-order bit
int a= -1; a = a >>> 24;
11111111 11111111 11111111 11111111 [-1]
00000000 00000000 00000000 11111111 [255]
38
Variable Arguments

39

You might also like