CSE 201 Java Lecture 2 TC
CSE 201 Java Lecture 2 TC
Java: Basics
Notes on Packages
Java classes are arranged in a hierarchy or package structure ` We can place our classes into packages, but it is optional ` When we do not specify any package for our class, the class is placed in a default unnamed package ` To place a class in a package we use the package statement ` A class without any access modifier has package scope only ` A public class from a package can be used across packages (i.e., can be used from a class of another package => the key is import declaration)
`
Thus, an import declaration is not required when one class in a package uses another in the same package
`
An Example
/* Filename: A.java */ package my_package; public class A{ private int a; public A(int a){ this.a = a; } public void setA(int a){ this.a = a; } public int getA(){ return this.a; } } /* Filename: JavaTest.java */ import my_package.A; public class JavaTest{ public static void main(String[] args){ A a = new A(10); System.out.println(a.getA()); } }
Why no import is required for System class ` Because System resides in the package java.lang, which is automatically imported to every Java application. So, we can use all classes from java.lang in our program without explicitly using the import statement.
`
More on import The use of import is similar to the use of using namespace in C++.
`
To import all the classes from a package (e.g. java.util) we have to use import java.util.*;
`
Examining InputTest.java
in of System.in is a static member of class System. It represents the standard input (similar to stdin or cin) ` Scanner is a class inside the Java API and is located in the java.util package ` Scanner contains some useful methods to read different types of data from the standard input (e.g. the keyboard) ` We have to use the import statement to tell the compiler that we will use Scanner from java.util in our program. Otherwise javac will not recognize Scanner
`
We call the method nextInt( ) on the Scanner object input to read an integer from the keyboard
`
If we enter a non-integer value, an exception is thrown and the program terminates abnormally. Exception Handling is a major topic in Java and will be discussed elaborately later in the course
`
Integers
byte ` short ` int ` long
`
8 bit integer 16 bit integer 32 bit integer 64 bit integer 32 bit floating point number 64 bit floating point number
Real Numbers
float ` double
`
Others
char 16 bit, Unicode 2.1 character ` boolean true or false. false is not zero (0) in Java
`
Non-Primitive Datatypes
`
Non-primitive datatypes
Class Variables ` Array
`
Non-primitive types are also called Reference types ` Object Example class Cuboid{
`
private int l, w, h; public Cuboid(int a, int b, int c){l=a;w=b;h=c;} public static void main(String args[ ]){ Cuboid ob; // ob is a reference pointing to null ob = new Cuboid(10,20,5); // now the actual object is created }
Reference Types
`
Can store exactly one value of its declared type at a time. When another value is assigned to that variable initial value is replaced Primitive type instance variables are initialized by default
`
Instance variables of type byte, short, int, long, float, double char initialized to 0
Instance variables of type boolean initialized to false Note: Local variables are not initialized by default
Stores locations of objects in memory and are said to refer to objects. Objects that are referenced may contain many instance variables and methods Reference type instance variables are by default initialized to null
initialized by default
` `
Reference Types
`
Primitive type variables are handled by value the actual values are stored in variables and passed to methods
All objects and arrays are handled by reference the references are stored in variables and passed to methods
Java References
Java references are used to point to Java objects created by new ` Java objects are always passed by reference to other functions ` In Java you can never pass an object to another function by-value ` Java references act as pointers but does not allow pointer arithmetic ` We cannot read the value of a reference and hence cannot find the address of a Java object ` We cannot take the address of a Java reference
`
But, we can make a Java reference point to a new object ` by copying one reference to another
ClassName ref1 = new ClassName(); ClassName ref2 = ref1;
`
by creating a new object using new and placing the returned address to the reference in question.
ClassName ref1 = new ClassName(); // first object ref1 = new ClassName(); // another object, address of first object is lost !!!
We cannot place arbitrary values to a reference except the special value null which means that the reference is pointing to nothing.
ClassName ref1 = 100; // compiler error ClassName ref2 = null; // no problem
Trying to use a null reference causes unexpected results or Exceptions in many cases ClassName ref2 = null; ref2.methodName();
`
Type boolean
Java boolean type variables cannot be cast to another datatype
`
In Java, 0 and null are not the same as false and non-zero and non-null are not the same as true
`
`
Executing NameDialog
SumOfInt.java
/* FileName: SumOfInt.java */ import javax.swing.*; class SumOfInt { public static void main(String args[ ]) { String s1=JOptionPane.showInputDialog(null,"Enter 1st number:"); String s2=JOptionPane.showInputDialog(null,"Enter 2nd Number:"); int num1=Integer.parseInt(s1); int num2=Integer.parseInt(s2); JOptionPane.showMessageDialog(null,"Sum is : " + (num1+num2)); } }
Executing SumOfInt
FirstWindow.Java (Contd.)
Container c = frame.getContentPane(); c.add(label1); c.add(topButton, BorderLayout.NORTH); c.add(bottomButton, BorderLayout.SOUTH); frame.setSize(800, 600); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
} }
Output of FirstWindow.java
Lecture Content
`
Chapter 2, 3, 4