Download presentation
Presentation is loading. Please wait.
1
Advanced Programming in Java
“Hello World” In Java Mehdi Einali
2
Agenda Review First program in java Variables Methods Conditions Loops
3
Review Java is Java is platform independent. Write Once, Run Anywhere!
Simple object oriented And popular Java is platform independent. Write Once, Run Anywhere!
4
First Program in java
5
Hello World Create a file named First.java Copy this lines to the file
Java class files have .java extension Note to naming convention Copy this lines to the file Note: File name and class name should be the same.
6
Setting JAVA_HOME Variable in Windows
From : Environment Variables For Java Applications 1. What are Environment Variables? Environment variables are global system variables accessible by all the processes running under the Operating System (OS). Environment variables are useful to store system-wide values such as the directories to search for the executable programs (PATH) and the OS version. Examples of environment variables in Windows OS are: COMPUTENAME, USERNAME: stores the computer and current user name. OS: the operating system. SystemRoot: the system root directory. PATH: stores a list of directories for searching executable programs. In setting up JDK and Java applications, you will encounter these environment variables: PATH, CLASSPATH, JAVA_HOME and JRE_HOME. In short: PATH: maintains a list of directories. The OS searches the PATH entries for executable programs, such as Java Compiler (javac) and Java Runtime (java). CLASSPATH: maintain a list of directories (containing many Java class files) and JAR file (a single-file archive of Java classes). The Java Compiler and Java Runtime searches the CLASSPATH entries for Java classes referenced in your program. JAVA_HOME and JRE_HOME: maintain the locations of JDK and JRE installed directory, respectively. 2. (Windows) How to Set or Change an Environment Variable Variables in Windows are NOT case-sensitive (because the legacy DOS is not case-sensitive). Environment variables are typically named in uppercase, with words joined with underscore (_), e.g., JAVA_HOME. Display Variables and their Values To list all the variables and their values, start a CMD shell (Click "Start" button ⇒ Run ⇒ Enter "cmd") and issue the command "set". To display a particular variable, use command "set varname". For examples, // Display all the variables (in NAME=VALUE pairs) prompt> set COMPUTERNAME=xxxxxxx OS=xxxxxxx PATH=xxxxxxx // Display a particular variable prompt> set COMPUTERNAME COMPUTERNAME=xxxxxx // OR use echo command with variable enclosed within a pair of '%'s prompt> echo %COMPUTERNAME% Set an Environment Variable To set an environment variable permanently in Windows (so that it is available to all the Windows' processes), start the "Control Panel" ⇒ "System" ⇒ (Vista/7/8) "Advanced system settings" ⇒ Switch to "Advanced" tab ⇒ "Environment variables" ⇒ Choose "System Variables" (for all users) or "User Variables" (for this login user only) ⇒ Choose "Edit" (for modifying an existing variable) or "New" (to create a new variable) ⇒ Enter the variable "Name" and "Value". Setting JAVA_HOME Variable in Windows In this example we will set the JAVA_HOME variable in Windows. The JAVA_HOME variable is used by applications to find the Java Development Kit installation. Find JDK Installation Directory First you need to know the installation path for the Java Development Kit. Open the default installation path for the Java Development Kit C:\Program Files\Java or C:\Program Files (x86)\Java There should be a subdirectory like C:\Program Files\Java\jre6 C:\Program Files (x86)\Java\jre6 Set the JAVA_HOME Variable Once you have the JDK installation path: Right-click the My Computer icon on your desktop and select Properties. Click the Advanced tab. Click the Environment Variables button. Under System Variables, click New. Enter the variable name as JAVA_HOME. Enter the variable value (one of the paths mentioned above) as the installation path for the Java Development Kit. Click OK. Click Apply Changes.
7
Debugging Syntax Error Run-time errors logic or semantic error
exceptions logic or semantic error The semantics, or meaning of the program, are wrong intelj hot keys Ctl + Space Invoke code completion. Alt + Enter suggestions for error Ctl + W* select word Ctl + F list methods. Alt + Insert smart insert Ctl + Alt + right/left arrow go to recent cursor places Alt + right/left arrow switch right/left tab Ctl + Tab switch tabs crl + e recent files Ctl + n find class Ctl + Shift + n find file Ctl + Shift + f find in files Ctl + y remove lines Ctl + d duplicate lines Shift + Alt + up/down arrow move line up/down F9 run/debug dialoge Shift + F9 debug Alt + F1 then 1 find file in navigator Esc go to main edit page Shift + Esc go to main edit page and close others F12 go to last opened window Alt+5 go to debug window
8
Java Programs A simple java program is a file
The file contains one class The class name equal to the file name The names are case sensitive The class contains a main method When we run the program, the main method is executed
9
Variables What is a variable? A piece of memory Holds data
For example a number, string or Boolean Java variables have a fixed size Platform independence
10
Java Primitive Types
11
Arithmetic Operators
12
Operator Precedence 1 + 2 * 3 = ? is treated as 1 + (2 * 3)
13
Equality and Relational Operators
14
Operators
15
Exercise
16
Programming strategy
17
Programming strategy Incremental Development
Start with a working program that does something visible, like printing something. Add a small number of lines of code at a time, and test the program after every change. Repeat until the program does what it is supposed to do
18
Recipe For Failure -1 Non-incremental development
If you write more than a few lines of code without compiling and testing, you are asking for trouble Attachment to bad code If you write more than a few lines of code without compiling and testing, you may not be able to debug it
19
Recipe For Failure -2 Random-walk programming Compiler submission
Make a change, run the program, get an error, make a change, run the program, etc The problem is that there is no apparent connection between the outcome of the program and the change. Compiler submission Error messages are useful, but they are not always right. Please think before edit
20
Khokhan-1
21
Khokhan-2 Stubborn Crazy Untidy Stochastic Hate other khokhans
And Love Chaos
22
end
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.