Upload
ruth-floyd
View
213
Download
0
Embed Size (px)
Citation preview
Java Overview
Comments in a Java Program
• Comments can be single line comments like C++Example: //This is a Java Comment
• Comments can be spread over multiple lines like CExample: /* This is a multiple line comment.
*/
• Comments can be special Java comments that help produce java documentation using the javadoc utitlity program. Example: /** comments for an HTML page goes here */
Identifiers
• Can be a series of characters consisting of letters, digits, underscores(_) and dollar signs ($).
• It does not begin with a digit and does not contain spaces and cannot be a reserved word.
Examples: WelcomeApplet, $value, _value, my_inputField, button8
Invalid Examples: 8button or
input field
Identifier Conventions
• Class names begin with a capital letter and have a capital letter for every word in the class name ( Ex: WelcomeAppletClass)
• Variables and Methods begin with lower case letter and have a capital letter for every word in the variable ( Ex: firstNumber, and myFirstMethod)
• Constants are all caps with an underscore separating individual words ( Ex: RATE_PER_HOUR)
Primitive Data Types and Declaring Variables
• Integer values (32 bits from -231 to 231-1): int number1, number2;
• Real values (32 bits): float area;
True/False values (8 bits): boolean done=true;
Primitive Data Types Continued
• Other Integers: byte (8 bits), short (16 bits) and long (64 bits)byte verySmallInteger;short numberInClass;long veryBigInteger;
• Other real values: double (64 bits)double nationalDebt;– By default, real values are double. For example, 5.0 is considered to be double not float. To make it float, write it as 5.0f
Number Literalsint i = 34;
long l = 1000000;
float f = 100.2f; or
float f = 100.2F;
double d = 100.2; or
double d = 100.2D;
or
Double d=100.2d;
Primitive Data Types Continued
• Character data – uses ISO Unicode Character set (16 bits)
• Established by the Unicode Consortium to support the interchange, processing, and display of texts for the world’s diverse languages. (see www.unicode.org)
– Ranges from ‘\u0000’ to ‘\uFFFF’– ASCII is a subset of Unicode (‘\u0000’ to ‘\u007F’
corresponds to the 128 ASCII charactersExamples: char letter='A'; //A char letter='\u0041'; //A char letterPI='\03A0'; //
Constants
static final datatype CONSTANT_NAME = valueOfConstant;
static final double PI = 3.14159;
static final int SIZE = 3;
Shortcut Operators
Operator Example Equivalent
+= i+=8 i = i+8
-= f-=8.0 f = f-8.0
*= i*=8 i = i*8
/= i/=8 i = i/8
%= i%=8 i = i%8
Increment andDecrement Operators
x = 1;
y = 1 + x++;
y = 1 + ++x;
y = 1 + x--;
y = 1 + --x;
Operator Precedence
• Casting• ++, --• *, /, %• +, -• <, <=, >, =>• ==, !=;• &&• ||• =, +=, -=, *=, /=, %=
Numeric Type ConversionConsider the following statements:
byte i = 100;long l = i*3+4;double f = i*3.1+l/2;
• When performing a binary operation involving operands of different types, Java automatically converts the operand of a smaller range type to a larger range type of the other operand.
• Example: If one is int and the other is float, the int is converted to float
Numeric Type Conversion
• Casting is an operation that converts a value of one data type into a value of another data type.
• Casting a variable of a type with a small range to a variable with a larger range is known as widening a type.
• Casting a variable of a type with a large range to a variable with a smaller range is known as narrowing a type.
• Widening a type can be performed automatically.• Narrowing a type must be performed explicitly.
Numeric Type Conversion
Examples:float f = 10.1; //illegal
float f = 10.1f; //legal
float f = (float)10.1; //explicit casting
The String Class
• java.lang.String is a class that models a sequence of characters as a string. The String class contains 11 constructors and > 40 methods
Examples:
String message = “Welcome to Java!”;
String class
• String – sequence of character data
• String objects are immutable – they cannot be changed once they have been created
• String class has 11 constructors– String ()– String (String s)
Strings
• Instantiating a string object– String name = “Joe”;– String name = new String (“Joe”);
String Methods
• char charAt (int position)– Return the character located at the specified
position•char c = name.charAt(2);
• equals– if (s1.equals(s2))
String Methods
• int length()– Returns the number of characters in the string
• int l = name.length();
• String substring (int start)• String substring (int start, int end)
– Used to extract a substring from a larger string
– start is the position of the first character to extract
– end is NOT the position of the last character to extract but the position following the last character to extract
String Methods
Assume: String letters = “abcdef”;• System.out.println (letters.substring (3));
– Prints def• System.out.println(letters.substring (0,3));
– Prints abc• System.out.println (letters.substring (2,4));– Prints cd
String Methods
• boolean equals (Object o)– Return true if the object is a string that has the
same length and contains the same characters as the string for which the method is called
• boolean equalsIgnoreCase (Object o)
String Methods
• int compareTo(String s)– Returns 0 if both strings are the same– Returns a positive number if the argument
string comes first– Returns a negative number if the calling string
come first
String Methods
indexOf and lastIndexOf – searches for a specified character or substring in a String
• Example: String letters =“abcdefabcd”;– int i=letters.indexOf(‘c’); // i contains 2– int i=letters.indexOf(‘$’); // i contains -1– int i=letters.indexOf (‘a’,1); //starts with
1 and searches for ‘a’ i contains 6– int i=lastIndexOf (‘b’); //i=7– int i=letters.indexOf(“def”); // i=3
String Methods
• replace (char,char), toUpperCase(), trim()– String s1 = “hello”;
– s1 = s1.replace (‘l’, ‘L’); //returns a string object in which every occurrence of ‘l’ is replaced by ‘L’
– s1 = s1.toUpperCase(); //”HELLO”– s1 = s1.trim() //remove any white spaces at
beginning or end