Primitive Types
Promotion in Expressions
Assignment Conversion
Type Casting
Arrays
Java Types and Default Values
Arrays with Multiple Dimensions
Strings
Arrays of Characters
String Buffers:
Operators
Flow Control
Classes
Exceptions
Packages, Classes, Interfaces, Methods, Fields, and Objects
Packages and Name Space Resolution
Class Modifiers
Method and Field Modifiers
Next Topic: Object Oriented Programming Concepts
class Test01 { public static void main(String args[]) { byte b = 25; b = b*4; System.out.println("Hello World! b = " + b); } } make Test01.class javac Test01.java Test01.java:4: Incompatible type for =. Explicit cast needed to convert int to byte. b = b*4; ^ 1 error *** Error code 1 make: Fatal error: Command failed for target `Test01.class'If you change byte to int everything works!
class Test02 { public static void main(String args[]) { int b = 25; b = b*4; System.out.println("Hello World! b = " + b); } } make Test02.class javac Test02.java java Test02 Hello World! b = 100
A narrowing conversion is only allowed if:
class Test03 { public static void main(String args[]) { byte b = 25; b = 100; System.out.println("Hello World! b = " + b); } }However, since Java is interpreted and there is no preprocessor, what do you think happens when you try to compile the following:
class Test04 { public static void main(String args[]) { byte b; b = 25*4; System.out.println("Hello World! b = " + b); } } make Test04.class javac Test04.java Test04.java:4: Incompatible type for =. Explicit cast needed to convert int to byte. b = 25*4; ^ 1 error *** Error code 1 make: Fatal error: Command failed for target `Test04.class'This is what happens under JDK 1.0x. However, the JDK 1.1.6 is smart enough to figure this out and it compiles it correctly.
However, if we make a small change:
class Test05 { public static void main(String args[]) { byte b; byte a=25; b = a*4; System.out.println("Hello World! b = " + b); } }We get the error even under JDK 1.1.6.
Does the following work?
class Test06 { public static void main(String args[]) { byte b; final byte a=25; b = a*4; System.out.println("Hello World! b = " + b); } }Yes!
Anything can be cast to a String
boolean values cannot be cast to anything else.
You can cast between any arithmetic types (integer, floating point, character)
Basically for objects, you can only cast if one is a subclass of the other.
All variables are given a value when they are created.
There are two classifications of types in Java.
All variables of reference type have default value null.
Unlike in C, arrays are created dynamically.
A declaration does not automatically create the array.
Arrays are created by new.
Array declarations which do not create an array:
int num[];Array declarations which do create an array:
int[] num;
int num[] = new int[10];Array declarations with run time creation:
int[] num = new int[10];
int num[] = {2,4,6,8};
int num[]; int size; size = 20; num = new int[size];The size of an array is known at run time.
Bounds checking is done on all array references.
A program can determine the size of an array:
class Test07 { public static void main(String args[]) { int num[]; int size; int i; size = 3; num = new int[size]; for (i=0; i < num.length; i++) num[i] = i*i; for (i=0; i < num.length; i++) System.out.println("num[" + i + "] = " + num[i]); } } java Test07 num[0] = 0 num[1] = 1 num[2] = 4
Consider the following array:
Point[] A; A = new Point[6]; for (int i=0; i < 6; i++) A[i] = new Point(i,i*i);Point is a simple class with the integer variables, x and y. It is initialized like this:
i x y 0 0 0 1 1 1 2 2 4 3 3 9 4 4 16 5 5 25What is the meaning of the following:
i x y 0 0 0 1 1 1 2 2 4 3 3 9 4 2 4 5 5 25What are the values after A[4].x = 50?
Click Here to run this applet.
import java.awt.*; import java.awt.event.*; import java.applet.*; public class Test08 extends Applet implements ActionListener { Point[] A; int A_size; Panel p; Button Init; Button Copy1; Button Copy2; Button Set2; Button Set4; public void init() { setLayout(new BorderLayout()); p = new Panel(); p.setLayout(new GridLayout(1,5)); p.add(Init = new Button("Init")); p.add(Copy1 = new Button("Copy 1")); p.add(Copy2 = new Button("Copy 2")); p.add(Set2 = new Button("Set 2")); p.add(Set4 = new Button("Set 4")); add("South",p); A_size = 10; A = new Point[A_size]; initialize(); Init.addActionListener(this); Copy1.addActionListener(this); Copy2.addActionListener(this); Set2.addActionListener(this); Set4.addActionListener(this); setBackground(new Color(245,245,245)); Init.setBackground(Color.cyan); Copy1.setBackground(Color.yellow); Copy2.setBackground(Color.yellow); Set2.setBackground(Color.pink); Set4.setBackground(Color.pink); repaint(1); } void initialize() { for (int i=0; i < A_size; i++) A[i] = new Point(i,i*i); } void copy_points_1(int i, int j) { A[i] = A[j]; } void copy_points_2(int i, int j) { A[i].x = A[j].x; A[i].y = A[j].y; } void set_points(int i, int x, int y) { A[i].x = x; A[i].y = y; } public void paint(Graphics g) { int y; y = 15; g.drawString("i",10,y); g.drawString("x",30,y); g.drawString("y",50,y); for (int i=0;i < A_size;i++) { y = i*15+30; g.drawString(i+":",10,y); g.drawString(A[i].x+" ",30,y); g.drawString(A[i].y+" ",50,y); } g.setColor(Color.blue); g.drawString("Init:",80,50); g.drawString("Initialize array to A[i].x=i and A[i].y=i*i",140,50); g.drawString("Copy 1:",80,75); g.drawString("A[4] = A[2]",140,75); g.drawString("Copy 2:",80,100); g.drawString("A[4].x = A[2].x and A[4].y = A[2].y",140,100); g.drawString("Set 2:",80,125); g.drawString("A[2].x = 10 and A[2].y = 30",140,125); g.drawString("Set 4:",80,150); g.drawString("A[4].x = 50 and A[4].y = 80",140,150); } public void actionPerformed (ActionEvent e) { if (e.getSource() == Init) { initialize(); } else if (e.getSource() == Copy1) { copy_points_1(4,2); } else if (e.getSource() == Copy2) { copy_points_2(4,2); } else if (e.getSource() == Set2) { set_points(2,10,30); } else if (e.getSource() == Set4) { set_points(4,50,80); } repaint(1); } }
C only has one-dimensional arrays, but you can have arrays of arrays giving you the functionality of multi-dimensional arrays. In C you would do something like this:
double matrix[10][20]; int i,j; for (i=0;i<10;i++) for (j=0;j<20;j++) matrix[i][j] = i+j;Some languages allow you to use multi-dimensional array syntax such as matrix[i,j] but not C or Java.
Like C, Java only has one-dimensional arrays.
class Test09 { public static void main(String args[]) { double matrix[][]; byte[][] bytematrix = {{2,3,4},{6,8,10}}; matrix = new double[3][12]; int i,j; System.out.println("matrix has lengths " + matrix.length + " and " + matrix[0].length); System.out.println("bytematrix has lengths " + bytematrix.length + " and " + bytematrix[0].length); for (i=0; i<2; i++) for (j=0; j<3; j++) System.out.println( "bytematrix[" + i + "][" + j + "] = " + bytematrix[i][j]); } } java Test09 matrix has lengths 3 and 12 bytematrix has lengths 2 and 3 bytematrix[0][0] = 2 bytematrix[0][1] = 3 bytematrix[0][2] = 4 bytematrix[1][0] = 6 bytematrix[1][1] = 8 bytematrix[1][2] = 10
class Test10 { public static void main(String args[]) { String str1 = "This is a string"; String str2; String str3; String str4; String str5; int i = 5; char fourth; System.out.println("str1: " + str1); System.out.println("str1 has length " + str1.length()); str2 = str1 + i; System.out.println("str2: " + str2); str3 = "This is" + " a string"; str4 = " a string"; str5 = "This is" + str4; System.out.println("str3: " + str3); System.out.println("str5: " + str5+"\n"); if (str1.equals(str3)) System.out.println("str1 equals str3"); else System.out.println("str1 and str3 are different"); if (str1 == str3) System.out.println("str1 == str3"); else System.out.println("str1 != str3"); if (str1.equals(str5)) System.out.println("str1 equals str5"); else System.out.println("str1 and str5 are different"); if (str1 == str5) System.out.println("str1 == str5"); else System.out.println("str1 != str5"); str1 = "This is a new string 1"; System.out.println("\nstr1: " + str1); if (str1.equals(str3)) System.out.println("str1 equals str3"); else System.out.println("str1 and str3 are different"); if (str1 == str3) System.out.println("str1 == str3"); else System.out.println("str1 != str3"); fourth = str1.charAt(3); System.out.println( "\nThe fourth character in str1 is " + fourth); } } str1: This is a string str1 has length 16 str2: This is a string5 str3: This is a string str5: This is a string str1 equals str3 str1 == str3 str1 equals str5 str1 != str5 str1: This is a new string 1 str1 and str3 are different str1 != str3 The fourth character in str1 is s
class Test11 { public static void main(String args[]) { int numargs; int i; numargs = args.length; System.out.println( "Number of command line arguments: " + numargs); for (i=0; i< numargs; i++) System.out.println(" " + i + ": " + args[i]); } } java Test11 Number of command line arguments: 0 java Test11 abc def ghij Number of command line arguments: 3 0: abc 1: def 2: ghij java Test11 abc "def ghij" Number of command line arguments: 2 0: abc 1: def ghij
class Test12 { public static void main(String args[]) { String str1 = "A string"; String str2; char arr[]; int strlen; int i; strlen = str1.length(); System.out.println("str1: " + str1); System.out.println("str1 has length " + strlen); arr = new char[strlen]; str1.getChars(0,strlen,arr,0); for (i=0; i< strlen; i++) System.out.println(" " + i + ": " + arr[i]); arr[2] = 'S'; str2 = String.copyValueOf(arr); System.out.println("str2: " + str2); } } java Test12 str1: A string str1 has length 8 0: A 1: 2: s 3: t 4: r 5: i 6: n 7: g str2: A String
class Test13 { public static void main(String args[]) { StringBuffer mystrbuf = new StringBuffer(); String str1 = "A string of any size"; String str2 = " Another string of any length"; String str3; System.out.println("str1: " + str1); System.out.println("str2: " + str2); mystrbuf.append(str1); mystrbuf.append(str2); str3 = mystrbuf.toString(); System.out.println("str3: " + str3); } } java Test13 str1: A string of any size str2: Another string of any length str3: A string of any size Another string of any length
Here are a few of the differences.
double arr[][]; outer: for (i=0; i < arr.length; i++) { for (j=0; i < arr[0].length; j++) { if (arr[i][j] == val) { foundit = true; break outer; } } }Like in C++, you can declare a variable in the initialization section of a for loop, but the scope of this variable is the body of the for loop.
Note the two constructors for the class Point.
class DPoint { protected double x; protected double y; DPoint (double x, double y) { this.x = x; this.y = y; } DPoint () { x = 0; y = -1; } public void show () { System.out.println("("+x+","+y+")"); } } class Test14 { public static void main(String args[]) { DPoint p1; DPoint p2; p1 = new DPoint(3.5, 4.7); p1.show(); p2 = new DPoint(); p2.show(); } } java Test14 (3.5,4.7) (0.0,-1.0)The class Point3D extends the class DPoint.
class Point3D extends DPoint { protected double z; Point3D(double x, double y, double z) { this.x = x; this.y = y; this.z = z; } public void show() { System.out.println("("+x+","+y+","+z+")"); } } class Test15 { public static void main(String args[]) { Point3D p1; p1 = new Point3D(3, 4, 9); p1.show(); } } java Test15 (3.0,4.0,9.0)
This cannot happen in Java.
In C under Unix, system errors are handled using errno.
Java handles these with exceptions.
A method which can cause an exception either handles it itself or can throw the exception to the calling method.
Examples of exceptions:
try { c = a/b; } catch(ArithmeticException e) { System.out.println("Divide by 0"); }
The code in an object is made up of methods.
The data is contained in fields.
A class is a description of the methods and fields of a collection of objects.
A constructor is a special method in a class which is used to initialize an object when the object is created.
An interface contains constants and method prototypes.
A class can extend another class and implement any number of interfaces.
A package is a collection of classes.