Courses‎ > ‎AP Computer Science‎ > ‎Konstantinovich‎ > ‎

2018-09-14

posted Sep 14, 2018, 6:17 AM by Konstantinovich Samuel   [ updated Sep 14, 2018, 7:16 AM ]
HW:
0. If you did not fill out the info collection form from the first day or two, you MUST do that.
1. Test the JDK by running this on a terminal:  
    javac -version
2. Compile and run a hello world program.
3. Make a new repository  MKS21X-Hello
4. Create a java file and add the blank file to your repo: Hello.java
After you add the file, write a java program that outputs :

Hello my name is x, y.

x should be replaced with your name
y should be replaced with the second part of your greeting, this is totally up to you, keep it clean!

You should have at least 3 commits. 1 created, 2 added empty file, 3 wrote code that works.

Languages:
  • Compiled Language
    • Human readable source code is translated (compiled) into machine language. (binary)
    • Machine language contains instructions that are run directly by the computer.
  • Source code and machine code are different files.
  • Java is a hybrid of compiled and interpreted language
  • Java programs compile into java byte code, that does not run directly on a computer, they go through the java virtual machine (JVM)
    • The JVM is like an interpreter. It translates from byte code to machine instructions.
  • Source files have a .java extension and are human readable. They _must_ end in .java 
  • Source files are compiled into java byte code by the compiler (javac) and are stored in files which have .class extension.
  • .class files are readable by the JVM
  • All .class files are cross-platform (can be run on any OS that has a JVM)
  • Each platform has a specific JVM. Mac, Windows, Linux, etc.


Comments:

Double forward slash will make the compiler ignore the text afterwards

//this is a comment

Printing
System.out.println( value );
//replace value with a value or expression

Note that variables have values, and functions that return values have values.
It is very easy to print a literal string with the text you want to appear.
e.g.
System.out.println("Please give me a drink.");
System.out.println("Here, have a Kuro Mitsu Matcha Latte");

Semicolons:
Semicolons are used to separate statements and are placed at the end of most statements. Exceptions will be discussed.

Variables : They store values, and you can change them by re-assigning after they are assigned. You cannot use a variable before it is assigned. 

declaration:
<type> <name>;
e.g.
int x;
float z;

assignment:
<name> = <expression>;
x = 3;
z = 2*3.14159+1;

declare + assign:
<type> <name> = <expression>;
char c = 'f';
String s = "whoa!";

Reassignment - can have the original variable in the expression:
x = x + 1; 
fish = fish + cat; //assumes fish and cat have values and are compatible with addition.



Compile and run code:
    javac is the command to compile a java source code (the java file)
    java is the command to run a java program (the class file. do not type .java)

e.g.
$javac Hello.java
$java Hello
Hello Human!


Java Files must be named to match the class names:

file: HelloWorld.java

public class HelloWorld{
    public static void main(String[] args){
        System.out.println("Hello world");
    }
}


Java runs the main method. You cannot place statements outside of methods.
Comments