0% found this document useful (0 votes)
4 views33 pages

Java Programming Basics and History

The document provides an overview of Java, including its history, structure of a source file, and instructions on how to write and run a Java program. It highlights Java's advantages such as platform independence and its application in various domains, while also addressing common errors encountered in programming. Additionally, it includes a simple example of a Java program and emphasizes the importance of debugging.

Uploaded by

farebaaminy08
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views33 pages

Java Programming Basics and History

The document provides an overview of Java, including its history, structure of a source file, and instructions on how to write and run a Java program. It highlights Java's advantages such as platform independence and its application in various domains, while also addressing common errors encountered in programming. Additionally, it includes a simple example of a Java program and emphasizes the importance of debugging.

Uploaded by

farebaaminy08
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program?

A First Program in J

Introduction to Programming
03-Introduction to Java Programming Language
(Your First Program)

Khadija Rahmany

Women Online University


Computer Science Faculty

April 17, 2024


Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

Table of contents
Review
Java History
What is Java?
Structure of a Java Source file
How to write and run a Java Program?
A First Program in Java: Printing a Line of Text
Error Categories
Think ON...
Reffrences
Question
Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

Review

1. How many types of computer Language do we have?


Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

Review

1. How many types of computer Language do we have?


2. What is software translator?
Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

Review

1. How many types of computer Language do we have?


2. What is software translator?
3. How many types of Software translator do we have?
Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

Review

1. How many types of computer Language do we have?


2. What is software translator?
3. How many types of Software translator do we have?
4. What is different between Interpreter and Compiler?
Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

Review

1. How many types of computer Language do we have?


2. What is software translator?
3. How many types of Software translator do we have?
4. What is different between Interpreter and Compiler?
5. What is bytecode?
Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

Review

1. How many types of computer Language do we have?


2. What is software translator?
3. How many types of Software translator do we have?
4. What is different between Interpreter and Compiler?
5. What is bytecode?
6. What is JVM?
Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

Java History

• Sun Microsystems funded an internal corporate research


project code-named Green in 1991.
• The project resulted in the development of a C and C++
based language that its creator, James Gosling, called Oak
base on oak tree outside his window at Sun.
• It was later discovered that there already was a computer
language called Oak.
• When a group of Sun people visited a local coffee place, the
name Java was suggested,from a list of random words.
Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

Java
Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

What is Java?

• Sun Microsystems released the first public implementation


as Java 1.0 in 1995
• It is an high level language and object orientated
programing language
• The language derives from the syntax of C and C++
• Java was designed with a concept of write once and run
everywhere
• On 13 November 2006, Sun released much of Java as free and
open source software under the terms of the GNU General
Public License (GPL).
Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

What is Java?

• Java can be used to create utilities, games, and business


applications .
• Java can also be used to create small programs, known as
applets, to be embedded in web pages
• Java runs on more than 850 million personal computers
worldwide
• Java runs on billions of devices worldwide, including mobile
and Networks.
Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

Advantages of java

• Platform Independence:Java compilers do not produce


native object code for a particular platform but rather’byte
code’instructions for the Java Virtual Machine(JVM). It
means they can be run on any operating system with any type
of processor as long as the Java interpreter is available on that
system.: Windows, Mac OS, Linux, whatever.
• Applet Interface:In addition to being able to create stand
-alone applications, Java developers can create programs that
can downloaded from a web page and run on a client browser.
• The java has API (Application Programming Interface)
• Java programs can be run on every systems a JVM exists for
without beeing recompiled
• It is faster than a real ’interpreter language’
Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

Disadvantages of java

• Java Programs are much slower than precompiled programs


written in other programing languages like C or C++
Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

Structure of a Java Source file

A source file contains source code that is written in the Java


programming language.

• Java Source file consist of pieces


called Classes.
• Classes consist of pieces called
Methods that perform tasks and
return information when they
complete their tasks
• A method contains Statements
Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

How to write and run a Java Program?

1. Create a source file


• A source file contains code, written in the Java programming
language,that programmers can [Link] can use any
text editor to create and edit source [Link] your source file
$ [Link]
2. Compile the source file into a .class file
• The Java programming language compiler (javac) takes your
source file and translates its text into instructions that the
Java virtual machine can [Link] the Java Compiler
with the command:
$ javac [Link]
3. Run the program
• uses the Java virtual machine to run your application.
$ java filename
Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

How to write and run a Java Program?

• Writing .java file


• Compile .java file ->.class file
• Execute .class file ->JVM
Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

A First Program in Java: Printing a Line of Text

1. Open you editor (jEdit or XEmacs or Eclipse)


2. Open a new file and save it [Link]
3. Type in the following the text and save it again
Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

HelloWorld-Class Definition

• Skeleton of any Java program is a class definition.


• Every Program in Java consists of a class definition, these are
called user-defined classes.
• The class keyword introduces a class definition in Java and is
immediately followed by a class name HelloWorld
• Keywords can only be used for intended purpose(s)
Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

HelloWorld-Class Definition

• In [Link] the class definition is:

class HelloWorld{
...
}
• In the Java language, the simplest form of a class definition is

class Name{
...
}
Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

Naming classes

• Name of class called identifier


• Try to use identifiers that ”tell the story” of the program.
• Series of characters consisting of letters, digits, underscores
( ) and dollar signs ( $ )
• Does not begin with a digit, has no spaces
• Examples: Welcome1, $ value, value, button7 ( 7button is
invalid)
• Java is Case Sensitive ( a and A are different)
• Capitalize every word (style) HelloWorld
Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

Blocks

• { } The left and right braces indicates the body of a class


definition or a method definition.
• Any code enclosed between braces is called a block.
• In the previous program there were two blocks.
• The class block (a block is required for a class)
• The method block (a block is required for a method)
• These are just two types of blocks. We will see others later in
the semester.
Good Programming Habit: Get into the habit of adding the right
brace as soon as you enter the left brace (then fill in between).
Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

HelloWorld-Main Method

• Every Java application must contain a main method, Without


the main method the Java Interpreter can not execute the
application, whose signature looks like this:

public static void main (String [] args)


• public: Accessability, everywhere accessable
• static: means that it is unique
• void: specifies the return value, void indicates that the method
doesnt return any value
• main: name of method
• String [] args:single argument in main method

• The parenthesis after main indicates that main is a program


building block called a Method.
Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

HelloWorld-Using

[Link](“Hello World!”);

• Instructs the computer to perform an action, namely to print


the string of characters contained between the double
quotation marks
• [Link]: Standard output object and used to print to
command window (i.e., MS-DOS prompt)
• [Link]: displays or prints a line of text inside
parenthesis.
Statement: The entire line including: [Link], the
argument in the parentheses(the string) and semicolon (;).
Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

A First Program in Java: Printing a Line of Text

1. Be sure that the class name is the same like your file name.
2. Compile the program in your console javac [Link]

3. Run your program in your console:java HelloWorld


Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

Errors

• Errors will occur when developing applications irrespective of


the target language.
• Programming errors are unavoidable, even for experienced
programmers.
• Errors can be categorized into these types:
• Syntax Errors
• Runtime Errors
• Logic Errors.
Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

Error Categories
Syntax Errors

Syntax errors:
• Caused when the rules of the language have not been
followed and the compiler cannot recognize a statement.
• Can be detect and picked up by the compiler.
• The compiler normally issues an error message to help the
programmer locate and fix it.
• Also called compile errors or compile-time errors.
• For example, if you forget a semi-colon, you will get a
syntax error.
Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

Error Categories
Runtime Errors and Logic Errors

• The compiler cannot pick up on runtime errors. Therefore


they happen at runtime.
• Runtime errors fall into two categories.
• Fatal Runtime Errors:
• Occur while the program is running if the environment
detects an operation that is impossible to carry out.
• These errors cause your program to crash.
• Logic errors
• Occur when a program doesn’t perform the way it was
intended to.
• The program can run but the results are not correct.
Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

Error Categories
Debugging

• In general, syntax errors are easy to find and easy to correct,


because the compiler gives indications as to where the errors
came from and why they are wrong.
• Runtime errors are not difficult to find, either, since the
reasons and locations of the errors are displayed on the
console when the program aborts.
• Finding logic errors, on the other hand, can be very
challenging.
• Logic errors are called bugs.
• The process of finding and correcting errors is called
debugging.
Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

Summery

• Java is an high level language and object orientated


programing language
• Platform Independence, Applet Interface and API are
Advantages of java.
• A source file contains source code.
• Errors will occur when developing applications irrespective of
the target language such as Syntax Errors, Runtime Errors
and Logic Errors.
Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

Think ON...

• Java Elements
Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

Reffrences

1. [Link]
2. [Link] [Link]
3. [Link]
[Link]
4. [Link]
[Link]
5. [Link]
6. [Link] 4798909 write-java-
[Link]
7. [Link]
[Link]/programming java [Link]
Review Java History What is Java? Structure of a Java Source file How to write and run a Java Program? A First Program in J

Question

You might also like