Lecture-03-Java-Part I

Download Report

Transcript Lecture-03-Java-Part I

Introduction to Java
Lecture Notes 3
Variables
l
A variable is a name for a location in
memory used to hold a value. In Java
data declaration is identical to c/c++
int n;
int m = 5;
double x;
Variables
with one modification, instead of const Java has final:
final int N = 10;
meaning that the value of N cannot be changed anymore.
Java is a strongly typed language, the value on the rhs
(right hand side) must be compatible with the type on
the lhs.
int x = 2.3;
is illegal, however
double x = 2;
is ok.
Types
Java has TWO general types: primitive types and reference
types. There are, correspondingly, two kinds of data
values that can be stored in variables, or passed as
arguments, or returned by methods, and operated on:
primitive values (or data) and reference values.
Primitive data - numbers and characters. In the
declaration
int n;
char ch = 'a';
int and char are types. Reference data - objects which
are defined by a class, for example
String str = "15-111";
Primitive Types
A primitive type is predefined by the Java programming
language and named by its reserved keyword. There are
the char type;
4 integer types: byte (1 byte), short (2 bytes), int (4
bytes), long (8 bytes), and char (2 bytes);
2 floating types: float (4 bytes), double (8 bytes);
the boolean type, an example,
boolean a;
Booleans have only two values: true and false.
Reference Types
There are three kinds of reference types: class types,
interface types, and array types. In the following
example of class
declaration
public class Demo
{ int x;}
Demo is a class type, and x is a field (or an instance
variables). How would you use the class type? To create
an object. An object is instantiated (created) class. We
need references to point to objects. Once we implement
a class Demo, we implement a
new type, so that we can declare
Demo tmp;
An Example
import javax.swing.JOptionPane;
public class Example1
{
public static void main(String[] args)
{
String s1, s2;
int n1, n2, max;
// prompt for integers
s1 = JOptionPane.showInputDialog("Enter an integer");
s2 = JOptionPane.showInputDialog("Enter another integer");
// convert these strings to integers
n1 = Integer.parseInt(s1);
n2 = Integer.parseInt(s2);
// find the maximum
max = Math.max(n1,n2);
// print the message and the max value
JOptionPane.showMessageDialog(null, "The biggest is " + max);
System.exit(0);
Exceptions
If the input string cannot be converted to an integer,
showInputDialog() throw an exception. We will discuss
exceptions in details much later.
try {
n1 = Integer.parseInt(s1);
}
catch (NumberFormatException a) {
System.out.println("Tne number is not integer');
System.exit( 0 );
}
Operator new
To create an object we use the new operator.
Integer n;
//declaration
n = new Integer(5); //instantiation
The act of creating an object is called instantiation. The
above two steps can be done in one
Integer n = new Integer(5);
String str = new String("This is a string");
The new method calls a constructor, which is a special
method of a class responsible for initialization.
Operator new
Note, specially for String, you can avoid calling new. It's done
implicitely. So the following operations are identical
String str = "This is a string";
String str = new String("This is a string");
Here is another example. There is a class DecimalFormat in
the package java.text which formats the unexact numbers.
DecimalFormat precision = new DecimalFormat( "0.00" );
The class DecimalFormat has a method format, that formats
the number to a particular form. Calling
System.out.println(precision.format(1.2345) );
will print "1.23".
Control Flows
if-statement as in c++
if (ch != 's' && total < MAX)
count++;
//same as count = count + 1;
else
count=/2; //same as count = count/2;
block statements
if (ch != 's' && total < MAX)
{
count++;
System.out.println("for debugging");
}
else
count=/2;
Control Flows
conditional operator as in c++. Here is the above example
written as the conditional operator.
count = (ch != 's' && total < MAX) ? count+1 : count/2;
The conditional operator is a great tool to separate plural and
single forms. Here is an example from the previous exercise
(an
exercise on converting miles to kilometers):
JOptionPane.showMessageDialog(null,
ml + " mile" + ((ml==1)?"":"s") + " is " +
km + " kilometers")
Control Flows
while-loop as in c++
while (true) // cannot be while(1)
{
System.out.println("a number");
System.exit(0);
}
One of the most common use of the while-loop is to validate
input.
Control Flows
switch-statement as in c++
switch (token)
{
case 'a': count1++; break;
case 'b': count2++; break;
default: System.out.println("error...");
}