Today... Java basics S. Bowers 1 of 8
Java main method (cont.) In Java, main looks like this: public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); Q: How is the above different than in C++? main is defined within a class main takes an array of Strings (command-line args) the [] s are in the wrong place! main s return type is void main is declared to be public and static Java methods declare their visibility (instead of sections in C++) static is the same here as in C++ (method lives with the class) the class is declared public classes in Java can have public or package (default) access instead of cout we use System.out.println(...) method S. Bowers 2 of 8
In C++: #include <iostream> using namespace std; int main(int argc, char **argv) { cout << "Hello World!" << endl; // or: char *argv[] In this statement... System.out.println("Hello World!"); System is a standard Java class out is a public, static variable (field) of System println is a method of the object out print is similar, but doesn t include a newline Memorize this! Public static void main string bracket bracket args S. Bowers 3 of 8
Working with String objects The String class is a lot like string in C++ String s = "Hello"; // create a new string object s System.out.println(s); // print "Hello" s.length(); // returns 5 char e = s.charat(1); // returns char e s.indexof(e); // returns 1 here (-1 if not found) String h = s + " " + "World!"; // string concatenation System.out.println(h); // prints "Hello World!" Note: these are hints for HW 1 To find out more about String, see the Java API! http://docs.oracle.com/javase/6/docs/api/ http://docs.oracle.com/javase/6/docs/api/java/lang/string.html Compiling and running To compile and run on ada: $ javac HelloWorld.java $ java HelloWorld Hello World! S. Bowers 4 of 8
Java Data Types Like C++, all variables must have a declared type Java distinguishes primitive from non-primitive types The primitives are the atomic values (numbers, characters, etc.) largely the same as C++, e.g.: int, char, double, etc. some differences though e.g., boolean s are true or false (instead of 0 and not 0, as in C++) Q: How would we define a public class method that always returns true? Note: class methods are static, instance methods are non-static public static boolean alwaystrue() { return true; Array and reference types (think objects) are the non-primitives more on these later S. Bowers 5 of 8
Exercise Write a public class method that: takes two String values (str1 and str2) and returns true if they start with the same character and returns false otherwise public static boolean samefirstchar(string str1, String str2) { if(str1.charat(0) == str2.charat(0)) return true; else return false; Now write a public class method that checks if all characters match public static boolean samestrings(string str1, String str2) { if(str1.length()!= str2.length()) return false; for(int i = 0; i < str1.length(); i++) if(str1.charat(i)!= str2.charat(i)) return false; return true; Note that these are missing the case when args are null (more later) S. Bowers 6 of 8
Javadoc Comments Java has three types of comments: one-line comment: // one-liner int v = 100; // v is velocity in mph standard multi-line comment: /* latte mocha dopio usually like this in Java: /* * latte * mocha * dopio a javadoc (multi-line) documentation comment /** * The velocity in mph *... note the double ** s on the first line S. Bowers 7 of 8
Doc comments used for class, interface, field, constructor, and method comments Doc comments have special syntax! The first paragraph is the description The following lines are block tags (or just tags) tags are denoted by a leading @ character the first one ends the description An example: /** * The <code>helloworld</code> class implements * the Java Hello World program. * @author Shawn Bowers * @version Assignment 0, 1/24/2012 public class HelloWorld { /** * Prints the hello world string * @param args the command line arguments (not used) public static void main(string[] args) { System.out.println("Hello World!"); S. Bowers 8 of 8