Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Big Java Chapter 1
Introduction to Java
A so-called high-level language
Also an object-oriented language
Developed by Sun in early 90’s
Release of version 1.0 in 1995
Original intention – use in embedded systems
Not so hard and complex as C++
Not so primitive as Visual Basic
Distinct feature – is not compiled directly to CPU language (assembler)
Intention was ”Write once, run anywhere”, platform-neutral
Need a layer in indirection, the Java Virtual Machine (JVM)
Java code is compiled to ”byte-code”, which is platform-neutral
The byte code is then interpreted by a platform-specific JVM
(Drawing)
Advantage: Need not be concerned with platform details
Drawback: Code which is interpreted runs slower than compiled code
In practice, Java programs are JIT-compiled, and cached
These ideas have been adopted by Microsoft in the .NET platform
Java is supported by a large library of methods
Big Java Chapter 1
Introduction to NetBeans
In order to write Java programs, we need a software ”environment”
Editor: For writing and editing our programs
Compiler: For compiling our Java programs
Debugger: For finding errors in our programs
Executor: For being able to run the actual programs
All of this is ”packaged” into a single product; NetBeans IDE
IDE: Integrated Development Environment
An all-in-one package, which is open-source and free
In order to use NetBeans, it must be
Downloaded
Installed
Check for Netbeans on your school PC
Should be version 6.1 or 6.5, if not, download and install
Download from
www.netbeans.org (choose Java SE for Windows), or
laerer.rhs.dk/psl/netbeans
File size is about 40 mega-byte
Try to start up NetBeans…
Looks somewhat overwhelming, easy to ”get lost”
We only need a few bits initially
Lets try to boil it down to essentials
(Demonstration)
Big Java Chapter 1
Creating a new Java project
In order to write our first program, we start with an ”empty” IDE
(Demonstrate)
A program is created by creating a Java ”project”
A project will contain a number of files
Some of these files will contain Java code
Try to create a project called ”MyFirst” (demonstrate)
Choose File|New Project
In Categories, choose Java
In Projects, choose Java Application
Press Next
In Project Name, type ”MyFirst”
In Project Location, choose a good place for your projects, like on
your ”H:” drive, in a folder called ”NetBeansProjects”
Press Finish
At first, the newly created project may look confusing
We only need to concentrate on one file, main.java
Actually, we can compile and run the new project (F6, or triangle)
Does not do anything interesting…
Big Java Chapter 1
My first Java program
The empty project can be compiled and run
Does not do anything interesting yet
We need to add Java code ourselves
Lets add a single line of ”magic” code, and examine it afterwards
Add this line exactly (demonstrate)
System.out.println("Hello 1Q");
When you got it right, press F6 (or green triangle)
Program writes ”Hello, 1Q” in Output window
Examine the elements in ”Main.java”
Comments – can be removed for now (compiler does not care)
Package statement – magic…
Public class Main – each .java file will contain a public class
(revisit later, in chapters 2 and 3)
Public static void main – mostly magic. For now, this is
where we put our code
The statement we wrote!
Java code is a sequence of statements
Statements can look very different, always concluded with ”;”
This particular statement:
System – a class defined in the Java library (magic)
.out – a member of the System class (magic)
.println – a member of the out object (magic)
println enables us to print a sequence of symbols (a string)
String is enclosed by ””
Big Java Chapter 1
My first Java program – revisited
Just writing one statement caused a lot of things to happen
The Java IDE tries to help us, but it may seem confusing at first
What does the red ”!” to the left mean?
The text we have written is not yet valid Java code
A large box appears when I type the first ”.” after ”System”
The Java IDE suggets all those things you can write after the ”.”, which
will make a valid Java statement
If I type ”system.”, nothing appears
Java is case sensitive; ”system” is NOT the same as ”System”
Why is there a waved red line under my statement?
The statement is not valid Java yet, holding the cursor over the line may
give you a hint about the problem
I have written everything right, but there is still a waved line?
Did you miss the semi-colon (;) at the end?
Even when all is correct, some words are highlighted
Don’t worry, it is not important right now…
If you feel up for it, add some more statements of the same type, but write
something different in the input string
Big Java Chapter 1
Exercises
R 1.1
R 1.4
R 1.10
R 1.12
P 1.2
P 1.7 + 1.8 (extras)