Installing Java and Programming Basics - PDF

Summary

This document provides a lecture on installing Java and the basics of the Java programming language. It covers the installation process, Java program structure, identifiers, and more. It introduces the Java programming language and its key components. It's aimed at those new to Java programming.

Full Transcript

Installing Java on a Home machine For Windows Users: Download/Install: Go to https://www.oracle.com/java/technologies/downloads/#jdk23-windows Download the file: https://download.oracle.com/java/23/latest/jdk-23_windows-x64_bin.exe Save the file to the desktop and run it....

Installing Java on a Home machine For Windows Users: Download/Install: Go to https://www.oracle.com/java/technologies/downloads/#jdk23-windows Download the file: https://download.oracle.com/java/23/latest/jdk-23_windows-x64_bin.exe Save the file to the desktop and run it. The installer will install Java to: C:\Program Files\Java\jdk-23\ Once the installer is finished, you must set your computer's path before Java will work correctly. Installing Java on a Home machine (cont’d) Setting the Path: Go to Edit the System Environment Look under Advanced----Environment Variables. Click on "Path" and then click Edit and then New Add the following to the end of the Path: C:\Program Files\Java\jdk-23\bin Click OK. Click the "New" button. For the variable name, type: CLASSPATH For the value, type:.; NOTE: The above value in words is period semicolon; it is not a misprint. Click OK. Exit out of all the open windows Installing Java on a Home machine (cont’d) To test if java is installed correctly: Open up a command prompt To do this click on Start and then Run Type in cmd.exe and click Ok Type java and hit enter If you see something like "command not found", then java is not installed correctly Otherwise you should see a list of options for running java The Java Programming Language A programming language specifies the words and symbols that we can use to write a program A programming language employs a set of rules that dictate how the words and symbols can be put together to form valid program statements Java was created by Sun Microsystems, Inc then purchased by Oracle It was introduced in 1995 and has become quite popular It is an object-oriented language 26 Java Program Structure In the Java programming language: A program is made up of one or more classes A class contains one or more methods A method contains program statements These terms will be explored in detail throughout the course A Java application always contains a method called main 27 First JAVA program Comments //Lincoln.JAVA //demonstrates the basic structure of a JAVA application public class Lincoln { public static void main (String[ ] args) { System.out.println(“A quote by Lincoln”); System.out.println(“Whatever you are, be a good one.”); } } Class definition See Lincoln.java (page 26) A simple Java program Consider A simple but complete Java program This program Prints two sentences on the screen A quote by Abraham Lincoln Sample output: A quote by Abraham Lincoln Whatever you are, be a good one Dissecting the first Java program All Java applications Start with a class definition In this case Lincoln preceded by public class Have a main method which is where processing begins is always preceded by public, static, and void The previous example invokes another method (execute) println that prints a character string to the screen Enclosed in double quote characters (”) Comments Comments in a program are also called inline documentation They should be included to explain the purpose of the program and describe processing steps They do not affect how a program works Java comments can take two forms: // this comment runs to the end of the line 31 Identifiers Identifiers are the words a programmer uses in a program An identifier can be made up of letters, digits, the underscore character (_), and the dollar sign ex: $amount, digit7, Num_Boxes, … They cannot begin with a digit Java is case sensitive, therefore Total and total are different identifiers 32 Identifiers Sometimes we choose identifiers ourselves when writing a program Sometimes we are using another programmer's code, so we use the identifiers that they chose Often we use special identifiers called reserved words that already have a predefined meaning in the language A reserved word cannot be used in any other way 33 Reserved Words The Java reserved words: abstract default goto operator synchronized boolean do if outer this break double implements package throw byte else import private throws byvalue extends inner protected transient case false instanceof public true cast final int rest try catch finally interface return var char float long short void class for native static volatile const future new super while continue generic null switch 34 White Space Spaces, blank lines, and tabs are collectively called white space White space is used to separate words and symbols in a program Extra white space is ignored Programs should be formatted to enhance readability, using consistent indentation 35