Вы находитесь на странице: 1из 10

CS 50a- Data Structure Introduction to Java Java is a high-level object-oriented programming language developed by the Sun Microsystems.

Java is influenced by C, C++, Smalltalk and borrowed some advanced features from some other languages. The most important feature of Java is its byte code that can be interpreted on any platform including windows, Linux etc. One can also download it freely from the official website of Sun. Java-programming language was not only developed for the small devices but it can be found also in a variety of devices like cell phones, e-commerce application, PCs and almost all network or computing devices.

Things to consider before making java program


1. 2. Installed Java Development Kit (JDK) for running and compiling of programs Text editor like notepad

Important Terminologies Software Development Kit (SDK) SDK is a set of development tools that allow applications to be created for certain software
packages or platforms. It includes sample code and technical notes or other supporting documentation;

Java Development Kit (JDK) The JDK is the most widely used SDK and is an extension of the SDK responsible for writing and
running Java programs. JDK includes components that are a selection of programming tools.

Java Runtime Environment (JRE)


The Java Runtime Environment (JRE), also known as Java Runtime, is part of the Java Development Kit (JDK), a set of programming tools for developing Java applications. The Java Runtime Environment provides the minimum requirements for executing a Java application; it consists of the Java Virtual Machine (JVM), core classes, and supporting files

Java virtual machine (JVM)


A Java virtual machine (JVM) is a virtual machine capable of executing Java bytecode.

Installing JDK
Install the downloaded JDK After the installation, Open the Local Disk C Double Click Program Files Double Click Java folder Open JDK directory and double click bin directory or any content in the bin directory and

Right click appletviewer


click properties

Copy the location of the one you have right clicked

Genevev Galapon-Reyes CCIT Instructor

CS 50a- Data Structure

After copying the location, Right click My computer/Computer found in your Start menu, select
Properties, then click Advance tab In the Advance tab, click the

In

Environment

Variables

window

Click

New

button

In the New User Variable window ,Type path on the variable name text box and paste the location that you have copied on the Variable value text box, then click OK button.

2 3 1

To check if you have successfully installed the JDK.. Open command prompt Type javac If the picture below appeared on your screen, you have successfully installed the JDK and set everything necessary in making java program

JCreator JCreator is a powerful IDE for Java JCreator provides the user with a wide range of functionality such as: Project management, project templates, code-completion, debugger interface, editor with syntax highlighting, wizards and a fully customizable user interface With JCreator you can directly compile or run your Java program without activating the main document first. JCreator will automatically find the file with the

Genevev Galapon-Reyes CCIT Instructor

CS 50a- Data Structure main method or the html file holding the java applet, then it will start the appropriate tool.

The Mechanics of Creating a Java Program


Java Program: a set of instructions for the computer to follow. The source code. Data: data for the Java program Java Compiler: translates the program from Java to a language that the computer can understand. Compilers differ by make of machine and operating systems. Byte Code Program: the compiler translates Java into a language called byte-code. Byte code is the machine language for a hypothetical computer called the Java Virtual Machine. Byte Code Interpreter: the interpreter translates each instruction of byte code into instructions that the computer in use can execute. Machine Language: is the language the computer in use understands.

Genevev Galapon-Reyes CCIT Instructor

CS 50a- Data Structure

Parts of a Java Program


/************************************* *Project: LabOneA *Programmer: John Smith *Date: September 23 *Program Name: Hello.java **************************************/ import java.io.*; public class Hello { public static void main (String[ ] args) { System.out.println("This is a literal print."); System.out.print("Yea!"); System.out.println("Go Java!"); } }

Remember that Java is case sensitive. The CODE:


/************************************* *Project: LabOneA *Programmer: John Smith *Date: September 23 *Program Name: Hello.java **************************************/

Information about the code:


All programs should begin with a commentidentifying the purpose of the program and the programmer. /**documentation */ - documentation commenting. /* text*/ - is the format for block commenting. // text - is the format for single line commenting. Comment statements are ignored by the compiler. It is a message to the reader of the code. The import command tells the computer to find packages that will be needed in the execution of the program. The java.lang package is the only package imported automatically without an explicit command; all other packages need an import command. This package is used for input and output. (Packages are collections of classes (libraries) which contain portable Java bytecode files.) Every Java program is a class. The program starts with the name of the class. This name must be the same name as the . java file in your folder. In this case the file is saved as Hello.java.

import java.io.*;

public class Hello Class names must begin with a letter, an underscore or a dollar sign. Class names may contain only letters, digits, underscores and/or dollar signs. Class names may not use reserved words. Genevev Galapon-Reyes CCIT Instructor

CS 50a- Data Structure A set of French curly braces { } is needed for every class.

A main method is needed for execution to have a place to start. This main method gets executed public static void main (String[ ] args) first. The idea of static will be discussed later. The array of strings parameter is a necessity. { A set of French curly braces { } is needed for main. Output statements: the basic output statement in Java is the System.out.println( ) statement. The System.out.println( ) line will print what is between the double quotes" " (called a literal print) and move the printing cursor to the next line. The System.out.print( ) line will print what is between the double quotes and leave the printing cursor on the same line. Braces are closed to end the program.

System.out.println("This is a literal print."); System.out.print("Yea!"); System.out.println("Go Java!");

} }

Style Issues
Program Format:
import java.io.*; //printing a message on the screen //notice the format of the code public class HelloClass { public static void main (String[ ] args) { System.out.println ("Hello, Java world!"); System.out.println ("I plan to be a Java expert!"); } } Notice the format style that we will be using. Theindentations keep the code clearly visible and easy to read. It is possible, in Java, to write all of your code on one line -this is called free form style. Free form style is extremely difficult to debug at a later date and is nearly impossible for a programming team to decipher. We will NOT be using free form style.

Case sensitivity:
Genevev Galapon-Reyes CCIT Instructor

Java is very picky about your caps lock key. The three

CS 50a- Data Structure lines of code at the left, at first glance, may appear to all say the same thing. The Java compiler, however, will only execute the first line of code. Most Java code is written in smaller case and ALL reserved words (such as "if") MUST be written in smaller case. In Java, comments may be expressed in different forms. The comments beginning with // are single line comments. They can appear on a line by themselves, or they may follow other lines of code. The comments enclosed within /* and */ are used for longer comments that wrap around a line.

if (netpay > grosspay) If (NetPay > GrossPay) IF (NETPAY > GROSSPAY)

Comments:
import java.io.*; //printing another message on the screen //notice the commented lines public class HelloAgainClass { public static void main (String[ ] args) { System.out.println ("Hello!"); //first print System.out.println ("I just love this Java!"); } } /*sometimes comments are longer statements that wrap around to the next line on the screen*/

Blank Space:
import java.io.*; //notice the spacing in this code public class HelloStillClass { public static void main (String[ ] args) { System.out.println ("Java rocks!"); System.out.println ("A real space cadet!"); } }

The compiler ignores extra blanks between words and symbols. Blank lines between lines of code are also ignored. Notice the blank line between the two print statements. You cannot, however, embed blanks in identifiers. The use of blanks improves readability.

Reserved Words (Keywords) for Java

Genevev Galapon-Reyes CCIT Instructor

CS 50a- Data Structure Here are the reserved words for Java ( keywords ). You may not redefine any of these reserved words. These words ALWAYS appear in lowercase. You should not give methods or variables the same name as any of these keywords. abstract boolean break case catch char const continue default double else extends finally float for generic goto if import inner instanceof interface long native null operator outer private protected public return short static switch synchronized this throws transient try void volatile while

byte class do final future implements int new package rest super throw var

Examples of comments
There are 3 styles of comments:
/**documentation */ - documentation commenting. /* text*/ - is the format for block commenting. // text - is the format for single line commenting. Comment statements are ignored by the compiler. Comments are simply messages to the reader of the code. /************************************ *Project: Demo for Comments *Programmer: Mr. Data *Date: June, 2003 *Program Name: Comments.java *************************************/ import java.io.*; public class Comments { public static void main (String[ ] args) { System.out.println("Hi"); //message to user System.out.println("LabOneA"); //assignment } } /* if you want to write a detailed comment, and it wraps around the screen, use this block style of commenting. */

Genevev Galapon-Reyes CCIT Instructor

CS 50a- Data Structure

The CODE:
/************************************ *Project: Demo for Comments *Programmer: Mr. Data *Date: June, 2003 *Program Name: Comments.java *************************************/ import java.io.*; public class Comments { public static void main (String[ ] args) { System.out.println("Hi"); //message to user System.out.println("LabOneA"); //assignment } }

Information about the code:


All programs should begin with a comment identifying the purpose of the program and the programmer. For our course please use the style shown at the left. Comments can also be placed within the body of a program to act as documentation. These comments should be brief and to the point. This body prints Hi! on the screen and LabOneA on the screen.

The block form of commenting is /* if you want to write a detailed comment, and used to display lengthy it wraps around the screen, use this block style comments that wrap around the of commenting. */ screen.

Escape Sequences
The following is a table of escape

sequences to be used when printing in

Java. These statements are embedded within a literal print remark (they go between the quotes):
Sequence \n \b \t \\ \' \" Name New line Backspace Horizontal tab Backslash Single quote Double quote Meaning Moves to beginning of next line Backs up one character Moves to next tab position
Tab spacing is every 8 columns starting with 1. (Columns 9, 17, 25, 33, 41, 49, 57, 65, 73 ...)

Displays an actual backslash Displays an actual single quote Displays an actual double quote

System.out.println("\tGeorge\t\tPaul"); will tab, print George, tab twice more, and print Paul.

Genevev Galapon-Reyes CCIT Instructor

CS 50a- Data Structure

Variables
A variable is a named memory location which temporarily stores data that can change while the program is running. A final is a named memory location which temporarily stores data that remains the same throughout the execution of the program. It is a constantvariable in the program. The type of a variable indicates what kind of value it will store. The name of a variable is known as its identifier. A variable is given a value through an assignment statement. Java recognizes different data types of variables depending upon what kind of data they can contain. Java has eight built-in primitive data types designated by reserved words: byte short int long float double char boolean

Variables of different types occupy different amounts of memory space and are described as having different sizes. Of the eight primitive data types in Java, the four most commonly used are:double, int, boolean, and char . When you learn about objects, you will discuss the differences between primitives and objects.

Variables Most Often Used


Data Java Type Keyword Character char Byte byte Genevev Galapon-Reyes CCIT Instructor Kind of Value 1 character - Unicode integer Bytes of Memory 2 1 Range of Values not applicable -128 to127

CS 50a- Data Structure

Short integer Integer Long Integer Float

short

Integers

-32,768 to 32,767 (-215 to 215 - 1) -2,147,483,648 to 2,147,483,647 (-231 to 231 - 1) -9223372036854775808 to 9223372036854775807 (-263 to 263 - 1) 3.4e-38 to 3.4e38 positive and negative 1.7e-308 to 1.73e308 positive and negative not applicable

int

Integers

long

Integers Decimal values to 7 decimal digit precision Decimal values to 15 decimal digit precision Boolean (Logical) values True or False

float

Double Boolean

double bool

8 1

Rules for assigning variables:


Assign short, int or long data types when you are sure a variable is a whole number (NO decimal points). Which type you choose depends upon the size of the numbers. Assign float or double when decimals are needed. Which type you choose depends upon the size of the numbers. Assign char if the variable will always contain only ONE character of data.

Genevev Galapon-Reyes CCIT Instructor

Вам также может понравиться