Welcome to DrJava. Working directory is C:\Users\st10 //======== // NOTE: I added many additional comments to these in-class projections // after class, and also added some blank lines for readability. //======== // DrJava (one of many Java integrated development environments (IDEs) // has an interactions window that allows you to play around // with individual Java expressions and statements -- that's // what we are doing here; // (type an expression or statement at the DrJava Interactions // window prompt -- the > -- and when you type return, // you are shown the value of that expression, the output // to standard out, etc.) > 3 + 5 8 //======== // Java does have quite a few similarities to C++ -- // you still have to declare identifiers before you use them, // and declarations of local variables of primitive types // are a lot like C++: > x = 3; Static Error: Undefined name 'x' > int x; > x = 3; > x 3 > int amount = 346; > amount 346 // (Java primitive types include int, double, float, char, // boolean, and a few others) //======== // like in C++, you use = for assignment, and == for comparison // for equality > amount == 346 true //======== // important Java difference: boolean is a distinct type, // with two literal values, true and false // it is NOT an integer underneath!! // and when Java expects a boolean expression, ONLY an // expression of type boolean will do! //======== // Many of Java's control structures are VERY similar to // those in C++ -- except, if they expect a boolean // expression, they REALLY expect it: // (oh -- and note Java's basic console output/standard output // method in the examples below, System.out.println // (println ends with a newline, print does not)) > if (amount > 345) { System.out.println("Moo"); } Moo > if (amount = 346) { System.out.println("Doesn't matter"); } Static Error: Condition must be boolean > if (amount == 346) { System.out.println("Hi"); } Hi > while (amount < 500) { amount += 50; System.out.println("amount is now: " + amount); } amount is now: 396 amount is now: 446 amount is now: 496 amount is now: 546 > for (int i=0; i<10; i++) { System.out.println("Hi there!"); } Hi there! Hi there! Hi there! Hi there! Hi there! Hi there! Hi there! Hi there! Hi there! Hi there! //======== // more playing with Java boolean expressions > boolean flag; > flag = true; > flag true > flag = false; > false false > flag = 1; Static Error: Bad types in assignment: from int to boolean > flag = 0; Static Error: Bad types in assignment: from int to boolean > flag == false true > flag == 0 Static Error: Illegal comparison: cannot compare a boolean to a int > flag != true true //======== // yes, < > <= >= != == DO all return true boolean results // && for logical and, || for logical or > flag && true false > flag || true true > flag and true Invalid top level statement //======== // C++ is multi-paradigm -- you can choose a purely // procedural approach, and/or you can go // object-oriented; // Java is object-oriented!! ALL code is within // classes or class-like constructs // even a Java application is a class! // // in C++, a program is any collection of functions/classes // with at least one named main // in Java, a Java class is an application IF it includes // a method named main; // when you execute that Java application, action starts // at that main method // // also: in Java, EVERY class is a descendant of a master // superclass named Object // (Java style: every class name starts with an uppercase letter, // and you use camelCase "within" that class name -- e.g., BasicMotor // // ALSO: * constructor methods have the SAME name as the class, // * otherwise, method names are expected to start with // a lowercase letter -- e.g., toString // * variable names start with a lowercase letter, also; // * and all are camelCase EXCEPT // * named constants, written in all-uppercase, still // used underscores _ to separate "words"; e.g., MAX_QUANT) //======== // in Java, String is a provided class > String moniker; > moniker = "Robotics"; > moniker "Robotics" //======== // you call the methods of a class instance -- of an object -- // as in C++: > moniker.length() 8 // (and Java considers anything in double-quotes to be // a literal String object -- so you CAN call methods on it!) > "moo".length() 3 //======== // BUT: in Java, every class instance is really a REFERENCE // (a "polite" pointer) to an instance of that class -- // if it isn't currently referencing, or poining to, // an object, its value is null. // (for a newly-declared class instance? Its value, then, // is null -- it isn't referencing anything yet.) > String name; > name null //======== // and, from C++, you should know you can't call a method // by following a pointer/reference that isn't pointing // at anything... > name.length() java.lang.NullPointerException // ...Java throws a NullPointerException in that case. > name = "Tobe"; > name "Tobe" > name.length() 4 //======== // in Java, classes are collected into packages, // if a class isn't explicitly noted as being in a package, // it is considered to be in the default package, // BUT many classes in the API -- Abstract Programming Interface -- // ARE in packages, and if you want to use the class // name of classes in those packages without preceding // them with the package name, you import the classes // in that package -- REALLY just telling the Java compiler // where to find them; // (then the compiler puts fully-qualified class names in the // bytecode!) // import <pkg_name>.*; let me give ALL the classes in // pkg_name without preceding them with pkg_name. // import <pkg_name>.<class_name>; // ...lets me JUST give THAT class without doing so; > import lejos.nxt.*; > import mooooo.*; > import lejos.nxt.Motor; Static Error: Undefined class 'lejos.nxt.Motor' // ...because DrJava DOESN'T have leJOS in its path, // doesn't know where to find its classes... //======== // USUALLY, to make a variable of a class type reference // a newly-created instance of a class, // you set it to the result of calling new // with a call to a class constructor method: > name = new String("George"); //======== // a static method or data field is one for which there is // EXACTLY one instance, no matter how many (or how few) // instances of that class there are; // // one way to call a static method: // ClassName.methodName(argument, argument, ...) // // for example, Math has a number of static methods that // should look familiar to a C++ programmer: > Math.abs(-4) 4 > Math.sqrt(2) 1.4142135623730951 //======== // C++ doesn't really care about the name of file containing // C++ source code -- except for an appropriate suffix; // Java cares! (It wants to be able to grab new code, more // classes, while a program is running!) // for Java source code, there can be multiple classes // in a source code file, BUT exactly 1 will be a public // class, and the file must be named that public class // name with a suffix of .java // for a public class Shape, Shape.java // ONCE you compile that class, // you get Java bytecode, ready to be interpreted and executed // by a Java virtual machine, // EACH class gets its own .class file; // (compiling into bytecode will be a LITTLE different // with leJOS, and there's an extra step or two, // as we will see later) //======== // (see posted example application class HelloWorld444.java // posted along with these notes) // to compile Java source code into bytecode, // (each class in a Java source code file gets a // resulting .class file of bytecode), // use javac with the *file* name // // for example, // javac HelloWorld444.java // // then, if it is a Java application class, you can execute // the resulting application (starting at its main method) // using java with the application *class* name // // for example, // java HelloWorld444 //======== // arrays in Java -- a lot like in C++, // except they are somewhat object-like, // and have a length data field including // the number of elements in that array // // desiredType desiredName[]; // desiredName = new desiredType[desiredSize]; > String manyNames[]; > manyNames null > manyNames = new String[5]; > manyNames { null, null, null, null, null } > manyNames[0] = "Peter"; > manyNames[1] = "Samuel"; > manyNames[2] = "Grace"; > manyNames[3] = "Hopper"; > manyNames[4] = "Turing"; > manyNames { Peter, Samuel, Grace, Hopper, Turing } > manyNames[1] "Samuel" > manyNames.length 5 > for (int i=0; i<manyNames.length; i++) { manyNames[i] += "!"; } > manyNames { Peter!, Samuel!, Grace!, Hopper!, Turing! } // yes, this works: > x = 7; > manyNames = new String[x]; > manyNames.length 7 >