Java Foundations: Unit 3 Parts of a Java Program
class + name public class HelloWorld public static void main( String[] args ) System.out.println( Hello world! ); A class creates a new type, something which can contain data and perform actions. In Java nothing can happen outside a class. Every class has a name, such as Turtle.
public + class public class HelloWorld public static void main( String[] args ) System.out.println( Hello world! ); Declaring a class public makes it useable by any other class. Classes can also be private.
Class-level Braces public class HelloWorld public static void main( String[] args ) System.out.println( Hello world! ); Java uses braces to group things together. In this case they delimit the start and end of the class declaration.
main public class HelloWorld public static void main( String[] args ) System.out.println( Hello world! ); When you start a program, Java has to know where to begina execution. Given the command: java HelloWorld Java looks for a class file named HelloWorld; within the class file it looks for a main method, and starts the program there.
public main public class HelloWorld public static void main( String[] args ) System.out.println( Hello world! ); Declaring a method public means it can be used by anyone. Methods may also be private.
static main public class HelloWorld public static void main( String[] args ) System.out.println( Hello world! ); Declaring a method static means it can be used at any time. If you don t declare a method static it can only be invoked on a specific object. Static methods are called class methods.
void main public class HelloWorld public static void main( String[] args ) System.out.println( Hello world! ); In Java every method has a type, and many are associated with values of that type. Math.log is type double, and computes the logarithm of a number: double exponent = Math.log( datapoint ); Void means that a method cannot be associated with a value.
String[] args public class HelloWorld public static void main( String[] args ) System.out.println( Hello world! ); When executing a program you can supply it with input via command line arguments. The Windows command > notepad HelloWorld.java has one command line argument. The Java command > java MyApp dick jane sally has three command line arguments which can be accessed through args.
Indentation public class HelloWorld public static void main( String[] args ) System.out.println( Hello world! ); In Java you can (usually) put white space and line breaks wherever you like. Use indentation to delimit groupings of code in your program.
Brace Alignment public class HelloWorld public static void main( String[] args ) System.out.println( Hello world! ); Aligning matching braces makes it easier to decipher.
Exercises 1. Go into your HelloWorld source code and change main to sam. Compile it with javac. What happens when you try to execute it with java? 2. In your HelloWorld program, add the code for GetGreeting (below), and change the println as follows: public class HelloWorld static public void main( String[] args ) System.out.println( getgreeting() ); static public String getgreeting() return "Hi there, world"; Explain: What happened when you substituted getgreeting() for "HelloWorld"? What is the purpose of String in the declaration of getgreeting? What is the purpose of static?
Strings
String Objects: Immutability A String object stores a sequence of Unicode characters. Strings are immutable; once created they cannot be changed. String bob = abra cada bra ; System.out.println( bob.trim() ); System.out.println( bob ); The trim method creates a new String object that contains the characters from the original String without the leading and trailing whitespace. The original String is not changed.
String Concatenation Java has special support for String objects. In particular, two strings can be concatenated by adding them together: String manny = the time has come, ; String moe = the walrus said, ; String jack = manny + moe; // ^^^^ // The time has come, the walrus said,
String Equality (1) Two different String objects can contain the same string of characters. String str1 = "Red dog, blue dog"; String str2 = "Red dog, blue dog"; String str3 = "Red dog, ";... 2526 Red dog, blue dog str3 = str3 + "blue dog";... 2734 Red dog, blue dog str1 and str2 refer to the same object str1 and str3 refer to the different object; however, both objects contain the same value
String Equality (2) To test two strings for equality, use the equals method in the String class. if ( str1.equals( str3 ) ) System.out.println( "equal" );
String Order To test the order of two Strings use compareto compareto evaluates to an integer: > 0, if the first string is greater than the second; 0 if the two strings are equal; or < 0 if the first string is less than the second: String onestr; String twostr;... int order = onestr.compareto( twostr ) if ( order > 0 ) System.out.println( onestr greater ); else if ( order == 0 ) System.out.println( strings equal ); else if ( order < 0 ) System.out.println( twostr greater );
String Collation String one is less than String two if one comes in the dictionary before two. Java uses a dictionary in which order is determined by unicode value: All integers are less than all letters. All upper case letters are less than all lower case letters. 42 is less than elephant elephant is greater than Zulu
Common String Methods Method charat Action Return the unicode character at a specified index. comparetoignorecase Perform a case-insensitive comparison of two strings. endswith indexof substring tolowercase trim Returns true if one string ends with another. Returns the index of a character within a string. Returns a substring of a string. Evaluates to a new string in which all characters in the old string are changed to lower case. Removes whitespace from the beginning and end of a string.
String Class Documentation See the complete documentation for the String class in the Java API.
Exercises 1. Given the following pairs of string literals, write a program to determine which member of the pair is greater than the other. 200 and 30 400 and 40 Zulu and Albatross Market and market If you don t know how to form an if statement, just print the value of the comparison and eye-ball it to see what the relative order is: System.out.println( 200.compareTo( 30 ));
StringBuilder (1) Because strings are immutable, dynamically building a string can be a very expensive operation: String prefix = "Rev4.2\t"; String author = "\tbob"; String body = "Dow Jones down 2%"; String message = prefix + body + author;
StringBuilder (2) StringBuilder provides a mutable class for dynamically assembling strings: String prefix = "Rev4.2\t"; String author = "\tbob"; String body = "Dow Jones down 2%"; StringBuilder bldr = new StringBuilder(); bldr.append( prefix ); bldr.append( body ); bldr.append( author ); String message = bldr.tostring();
StringBuilder (3) See the complete StringBuilder class documentation.