Professional Documents
Culture Documents
Java Basics Ppt2
Java Basics Ppt2
Java Basics Ppt2
MCA-501(N2)
Java Basics
Data Types
Java has two main categories of data types:
Primitive data types
Built in data types
Many very similar to C++ (int, double, char, etc.)
Variables holding primitive data types always hold the actual
value, never a reference
Characters
char 16 bits, represented in unicode, not ASCII!
Boolean data
boolean
can only have the value true or false
Unlike C++, cannot be cast to an int (or any other data type)
Operators
Arithmetic
+,-,*,/,%,++,--
Logic
&,|,^,~,<<,>>,>>>
Assignment
=, +=, -=, etc..
Comparison
<,<=,>,>=,==,!=
Comparison Operators
Note about comparison operators
Work just as you would expect for primitive
data types
We will see that they do not always operate
as you would think for reference data types
Control Structures
if/else, for, while, do/while, switch
Basically work the same as in C/C++
i = 0;
while(i < 10) {
a += i;
i++;
}
i = 0;
do {
a += i;
i++;
} while(i < 10);
> 3) {
= 3;
switch(i) {
case 1:
string = foo;
{
case 2:
= 0;
string = bar;
default:
string = ;
}
Internet And Java Applicat
6
ion
Control Structures
Java also has support for continue and break
keywords
Again, work very similar to C/C++
for(i = 0; i < 10; i++) {
a += i;
if(a > 100)
break;
}
Example
Variable declarations:
int primitive = 5;
String reference = Hello;
Memory representation:
primitive
reference
5
Hello
Arrays
In Java, arrays are reference data types
You can define an array of any data type
(primitive or reference type)
Special support built into the language for
accessing information such as the length
of an array
Automatic bound checking at run time
Internet And Java Applicat
ion
10
11
12
13
Wrapper Classes
Each primitive data type has a
corresponding Wrapper Class reference
data type
Can be used to represent primitive data
values as reference objects when it is
necessary to do so
Byte, Short, Integer, Long,
Float, Double, Boolean,
Character
Internet And Java Applicat
ion
14
15
Example
public class Circle {
static final double PI = 3.14;
double radius;
public double area()
{
. . .
}
Each instance of a
Circle object contains
the fields and methods
shown
static
Example
Static member variable (field) of the System class:
PrintStream object that points to standard out
System.out.println(Hello World);
Built in Java class
17
18
Contents
1. The String Class
2. The StringBuffer Class
3. The Character Class
4. The StringTokeniser Class
5. Regular Expressions
19
Strings
In Java, Strings are reference data types
They are among many built-in classes in
the Java language (such as the wrapper
classes)
However, they do not work exactly like all
classes
Additional support is built in for them such as
String operators and String literals
Internet And Java Applicat
ion
20
21
3
4
22
Creating Strings
As mentioned before, creating reference
objects in Java requires the use of the
new operator
Strings can be created this way:
String myString = new String(foo);
23
String Operators
The + operator contains special support
for String data types:
myString = foo + bar;
24
Comparing Strings
As mentioned before, the == operator does not
always work as expected with reference data
types
Example:
String string1 = foo;
String string2 = string1;
if(string1 == string2)
System.out.println(Yes);
25
Solution
The String class contains built in methods that
will compare the two strings rather than the two
references:
String string1 = foo;
String string2 = string1;
if(string1.equals(string2))
System.out.println(Yes);
String string1 = foo;
String string2 = string1;
if(string1.equalsIgnoreCase(string2)
)
System.out.println(Yes);
Internet And Java Applicat
ion
26
Comparing Strings
0
48
57
65
90
97
122
s1.equals(s2)
lexicographical comparison
returns true if s1 and s2 contain the same text
s1 == s2
continued
27
s1.compareTo(s2)
28
29
30
31
32
33
34
35
36
37
Just a starting
value, the size
can dynamically
increase.
38
continued
39
buf1.append(hello)
appends hello to the end of buf1
append() can take many types of
arguments: int, char, float, etc.
buf1.insert(5, hello)
modify
buf1
40
41
continued
42
43
44
Compare with:
char c2 = A;
45
46
continued
47
StringTokenizer st =
new StringTokenizer(...)
48
4.2. TokenTest.java
import java.util.*;
// StringTokenizer class
import javax.swing.JOptionPane;
public class TokenTest {
public static void main(String args[])
{
String str = JOptionPane.showInputDialog(
"Enter a string:");
String output;
StringTokenizer tokens =
new StringTokenizer( str );
:
Internet And Java Applicat
ion
49
50
Use
51
Notes
tokens.countTokens()
52
5. Regular Expressions
A regular expression is a text pattern.
The java.util.regex package contains
several classes that allow you to compare
strings to regular expressions to see if the
y match.
matching strings can be changed/replaced
after being found
Internet And Java Applicat
ion
continued
53
continued
54
55
import java.util.regex.*;
public class BasicMatch
{
public static void main(String[] args)
{
// Compile regular expression
String patternStr = "b";
Pattern pattern =
Pattern.compile(patternStr);
:
56
// "b"
// true
57
58
59
StringTokenizer
String text = To be or not to be;
StringTokenizer st = new StringTokenizer();
While(st.hasMoreTokens()) {
System.out.println(The next word is +
st.nextToken());
}
60