Docsity

Introduction to Java Programming: A Beginner's Guide, Slides of Logic

Partial preview of the text

Download Introduction to Java Programming: A Beginner's Guide and more Slides Logic in PDF only on Docsity!

Introduction to Java

James Brucker

What is Java?

Java is a language for writing computer programs.

it runs on almost any "computer".

 desktop  mobile phones  game box  web server & web apps  smart devices

How Java Works: Compiling

  1. You (programmer) write source code in Java.
  2. A compiler (javac) checks the code and translates it into "byte code" for a "virtual machine". Hello.java javac compiler Hello.class Java byte code Java source code public class Hello { public static void main( String [ ] args ) { System.out.println( "I LOVE programming" ); } } public Hello (); aload_ invokespecial #1; //Method java/ lang/Object."":()V return main(java.lang.String[]); getstatic #2; //Field ...

How Java Works: Run the Bytecode

  1. Run the byte code in a Java Virtual Machine (JVM).
  2. The JVM (java command) interprets the byte code and loads other code from libraries as needed. Hello.class java java virtual machine Java byte code public Hello (); aload_ invokespecial #1; //Method java/lang/Object."":()V return main(java.lang.String[]); getstatic #2; //Field ... Other java classes, saved in libraries. I LOVE programming

How to Write Source Code?

You can use any editor to write the Java source code. For your first program, use a text editor (e.g. Notepad). Later you will use an Integrated Development Environment (IDE) such as Eclipse, IntelliJ, Netbeans, or VS Code. Or a beginner's IDE: BlueJ. An IDE makes you much more productive, but you need some practice... so for now, use a text editor.

How to Get Java

To do these examples you must install the Java Development Kit (JDK). Useful to also install the Java documentation (Javadoc).

Implementations of Java

 (^) Oracle Java: java.oracle.com  (^) OpenJDK: AdoptOpenJdk.net  (^) Stick with LTS versions: Java 8, 11, 15

Installation Notes

 (^) There are some suggestions at the end of these slides  (^) You can also find videos on YouTube for help installing Java and writing your first program.

Check your Java Installation

  1. Open a command line window or "terminal" (Mac). This is where you will enter commands.
  2. Enter " java -version " and " javac -version "

C:>

C:> java -version java version "11.0.5" 2019-10-15 LTS If it prints "command not found", then the Java "bin" directory is not on your terminal's "search path", specified by the PATH environment variable.

Write this Program

  1. Use a text editor (WordPad, EditPlus, not MS Word).
  2. Save the file as Hello.java public class Hello { public static void main( String[ ] args ) { System.out.println( "Hello, human" ); } } Name of the class must be same as name of file

Compile and Run the Program

  1. Compile the program using " javac ". This translates the source code into byte code.
  2. Run it. " java " runs the virtual machine. C:> javac Hello.java C:> java Hello Hello, human.

2nd Version: read some input

In this version we'll see how to read input and use a local variable. The easiest way to read input in Java is with a Scanner object. A Scanner parses input into strings, numbers, etc To create a Scanner object for reading the console use: import java.util.Scanner; public class Hello { public static void main(String[] args) { Scanner console = new Scanner( System.in ); System.in is a predefined variable for byte-by-byte input from the default input (the terminal) this declares a variable of type Scanner named "console". import tells the compiler where to find the Scanner class

2nd Version: main method

Scanner has a method named nextLine( ) that reads the rest of the input line and converts it to a String. System.out.print("string") does not print a newline after the output. public static void main(String [] args) { Scanner console = new Scanner(System.in); System.out.print("What is your name? "); String who = console.nextLine( ); System.out.println("Hello, " + who ); } }

  • joins two strings together.

Compile and Run 2nd Version

Compile (javac) and run (java) the 2nd version: C:> javac Hello.java C:> java Hello What is your name? Nerd Hello, Nerd.

3nd Version: decisions

Most programs contain logic for decision making. We'll write a program that greets and shows the time. The logic is: get the current time if now is before 12:

print " Good Morning, " + user's name

else if now is before 18:00 then

print " Good Afternoon, " + user's name

else print " Good Evening, " + user's name

print " It is now hh:mm:ss" (show the actual time)