Chapter 1 What is Java? - made by Sun in 1995, similar to C and C++ - actually denotes a language and a platform What's a Platform? - it includes the libraries, developer kit, compiler, and Java Virtual Machine (JVM) - the first three are very important to writing and making a program - the JVM is what actually runs a program once it is created (not all languages have this) What's a programming language (PL)? - relation to human languages (HL): - similarities: both have punctuation, grammar, vocabulary, and a general way of expressing yourself - differences: programming languages are unambiguous, strictly enforce rules - syntax - what we call these rules in PL, basically a combination of HL grammar and vocab - important because a program must be syntactically correct to run - besides being syntactically correct, a program must be semantically correct - semantics relate to the meaning of a program, whether it runs the way it is expected to - bugs are often the result of semantic problems - besides HL, PLs borrow a lot from math - many of the first programmers and computer engineers were mathematicians - all the normal arithmetic operators are there: +, -, / (divide), * (multiply) - PL variables are very similar to math variables - graphics typically use the normal xy-coordinate system (or xyz for 3D), called Cartesian coordinates - trigonometry and linear algebra are used heavily - PL methods are very similar to the idea of a math function - PLs are something easy for humans to understand that get turned into machine-understandable code What's Java (the language)? - it is a compiled, statically typed language - very popular because its platform enables it to be cross-platform, simplifies some of the more confusing parts of C/C++ (like garbage collection - ask me about it later) - it is NOT javascript - its "big thing" is that it is completely Object Oriented (we'll get to this later) Hello World Revisited public class HelloWorld { public static void main( String args[ ] ) { System.out.println( "Hello World!" ) ; } } - public class HelloWorld defines a new Java class called HelloWorld - public static void main( String args[ ] ) defines a new function or method called main that takes an array of Strings as its arguments - main is the default beginning point when a program runs - System.out.println( "Hello World!" ) ; is the line that tells the JVM to output the thing between quotes to the screen - println( "Hello World!" ) is a method call - method calls have three parts: Object, method name, and parameters (optional) - the semicolon at the end is kind of like a period in English, it ends most statements - This won't all make sense yet, so don't worry - try changing some stuff.... what sorts of problems arise? - try 2+2 for example +[Exercise: P1.1]+ Whitespace and Case - it is important to note that Java is not spacing sensitive so "public class HelloWorld { public static void main(String args[]){ System.out.println("Hello World!") ; }}" is the same as above - so, line breaks and spaces can often be left to how you want your code to look - I advise using the conventions that I and the book use - note that "words" have to have space, "public class HelloWorld" is different from "publicclass HelloWorld" which is different from "public class Hello World", etc. - in terms of casing, Java *IS* case sensitive - "public class HelloWorld" is completely different from "public class helloworld" or "PUBLIC cLaSS HelloWorld" - casing conventions are used for variable, method, package, and class names... we'll get to these later - not having the casing correct will lead to syntax errors - ex. if you don't name your main method exactly as "main" (say you call it Main) Java will complain about not being able to find the main method Basic template of a program public class ClassName { // class variables go here, if any // now the methods, if any public void someMethod( ) { ... } // finally the main method public static void main( String args[ ] ) { ... } } Comments - comments inside code are lines of text that are NOT code, in other words not compiled and not part of the program - they are mostly written to explain code or concepts inside of the code - it is very important to comment your code!!!! - this especially becomes crucial when we learn about Javadoc - there are two types 1. Single line comments - these have "//" at the beginning of the line and only last for a single line - if you look above I have several of these in the "basic template of a program" 2. Multi-line comments - these start with "/*" and terminate with the first "*/" - something like: /* start my comment keep going more stuff System.out.println( "Hello World!" ) ; this won't do anything because it's in a comment now I'm done! */ +[Exercises: P1.2, P1.5, P1.8]+ Backups **IMPORTANT** - it is VERY easy to make changes that mess up your program - you don't want to get stuck in a situation where you cannot take them back - thus, I recommend backing up your project folder each day before you begin and before any major code changes - do this by opening konqueror, navigating to ~/workspace, right-clicking on the project folder, hit add to archive, and name it - you can name them by appending dates, for example if your project is Wackadot then name the backup Wackadot06162008.zip, if you have multiple backups in a day append _#, like Wackadot06162008_2.zip