25
Java Overview

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

Embed Size (px)

Citation preview

Page 1: 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

Java Overview

Page 2: 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

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 */

Page 3: 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

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

Page 4: 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

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)

Page 5: 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

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;

Page 6: 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

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

Page 7: 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

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;

Page 8: 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

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'; //

Page 9: 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

Constants

static final datatype CONSTANT_NAME = valueOfConstant;

static final double PI = 3.14159;

static final int SIZE = 3;

Page 10: 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

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

Page 11: 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

Increment andDecrement Operators

x = 1;

y = 1 + x++;

y = 1 + ++x;

y = 1 + x--;

y = 1 + --x;

Page 12: 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

Operator Precedence

• Casting• ++, --• *, /, %• +, -• <, <=, >, =>• ==, !=;• &&• ||• =, +=, -=, *=, /=, %=

Page 13: 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

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

Page 14: 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

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.

Page 15: 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

Numeric Type Conversion

Examples:float f = 10.1; //illegal

float f = 10.1f; //legal

float f = (float)10.1; //explicit casting

Page 16: 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

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!”;

Page 17: 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

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)

Page 18: 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

Strings

• Instantiating a string object– String name = “Joe”;– String name = new String (“Joe”);

Page 19: 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

String Methods

• char charAt (int position)– Return the character located at the specified

position•char c = name.charAt(2);

• equals– if (s1.equals(s2))

Page 20: 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

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

Page 21: 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

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

Page 22: 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

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)

Page 23: 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

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

Page 24: 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

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

Page 25: 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

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