22
Java program explanation • /* The Welcome class implements an application that simply prints "Hello World..!" to standard output. */ • class Welcome { • public static void main(String[] args) { System.out.println("Hello World..!"); // Display the string. •} •}

#_ varible function

Embed Size (px)

Citation preview

Page 1: #_ varible function

Java program explanation

• /* The Welcome class implements an application that simply prints "Hello World..!" to standard output. */

• class Welcome { • public static void main(String[] args)

{ System.out.println("Hello World..!"); // Display the string.

• }• }

Page 2: #_ varible function

Comments• Comments are ignored by the compiler but are useful to

other programmers. The Java programming language supports three kinds of comments:

• /* text */ The compiler ignores everything from /* to */. • /** documentation */ This indicates a documentation

comment (doc comment, for short). The compiler ignores this kind of comment, just like it ignores comments that use /* and */. The javadoc tool uses doc comments when preparing automatically generated documentation.

• // text • The compiler ignores everything from // to the end of the

line.

Page 3: #_ varible function

Java documentation

• Javadoc (originally cased as JavaDoc) is a documentation generator created by Sun Microsystems for the Java language (now owned by Oracle Corporation) for generating API documentation in HTML format from Java source code.

• Some IDEs, such as Netbeans and Eclipse, automatically generate Javadoc HTML.

Page 4: #_ varible function

Java documentation

• // import statements • /** • * @author Firstname Lastname <address @

example.com> • * @version 1.6 (current version number of program) • * @since 2010-03-31 (the version of the package

this class was first added to)• */ • class Welcome { // class body }

Page 5: #_ varible function

Javadoc tagsTag & Parameter Usage

@author John Smith Describes an author.

@version versionProvides software version entry. Max one per Class or Interface.

@since since-text Describes when this functionality has first existed.

@see reference Provides a link to other element of documentation.

@param name description Describes a method parameter.

@return description Describes the return value.

Page 6: #_ varible function

class

• The most basic form of a class definition is: class name { . . . } • The keyword class begins the class definition

for a class named name• The code for each class appears between the

opening and closing curly braces

Page 7: #_ varible function

public static void main(String[] args) {…}

• public means that the method can be seen outside of this class;

• static means that you don't have to create a new object;

• void means it doesn't return a value

Page 8: #_ varible function

public static void main(String[] args) {…}

• The keyword public is an access specifier. • The keyword static is a kind of modifier. • The keyword void means that the method main() does not return

any value. • As we had seen before all the java program will start execute by

calling the main method. • If we want to pass any information to a method will be received by

the variables declared within the parenthesis is called parameters. • In a main() method there is only one parameter ,String args[] .

args[] is a name of the parameter that is an array of the objects of data type String.

• String store sequences of characters and args will receive the command line arguments.

Page 9: #_ varible function

public static void main(String[] args) {…}

• The modifiers public and static can be written in either order (public static or static public), but the convention is to use public static as shown above.

• You can name the argument anything you want, but most programmers choose "args" or "argv".

• The main method is similar to the main function in C and C++; it's the entry point for your application and will subsequently invoke all the other methods required by your program.

Page 10: #_ varible function

System.out.println("Hello World!");

• Finally, the line:• System.out.println("Hello World!"); • uses the System class from the core library to

print the "Hello World!" message to standard output.

• This is included in Java.lang libraray.

Page 11: #_ varible function

static keyword

• The static keyword in Java means that the variable or function is shared between all instances of that class as it belongs to the type, not the actual objects themselves.

• So if you have a variable: • private static int i = 0; • and you increment it (i++) in one instance, the

change will be reflected in all instances. i will now be 1 in all instances.

Page 12: #_ varible function
Page 13: #_ varible function

Variable• You can call them almost anything you like, but there are a few rules:• Variable names can't start with a number. So first_number is OK, but

not 1st_number. You can have numbers elsewhere in the variable name, just not at the start.

• Variable names can't be the same as Java keywords. There are quite a lot of these, like int above.

• You can't have spaces in your variable names. The variable declaration int first number will get you an error. We've used the underscore character, but it's common practise to have the first word start with a lowercase letter and the second or subsequent words in uppercase: firstNumber, myFirstNumber

• Variable names are case sensitive. So firstNumber and FirstNumber are different variable names.

Page 14: #_ varible function

Java Keyword

Page 15: #_ varible function

Invalid Identifier

Page 16: #_ varible function

Accepting Input from a User

• One really useful class that handles input from a user is called the Scanner class.

• The Scanner class can be found in the java.util library.

• To use the Scanner class, you need to reference it in your code. This is done with the keyword import.

• import java.util.Scanner;

Page 17: #_ varible function

Accepting Input from a User• To create a new Scanner object the code is this:• Scanner user_input = new Scanner( System.in );• So instead of setting up an int variable or a String

variable, we're setting up a Scanner variable. We've called ours user_input.

• After an equals sign, we have the keyword new. This is used to create new objects from a class. The object we're creating is from the Scanner class.

• In between round brackets we have to tell java that this will be System Input (System.in).

Page 18: #_ varible function

Get the user input

• To get the user input, you can call into action one of the many methods available to your new Scanner object. One of these methods is called next. This gets the next string of text that a user types on the keyboard:

• String first_name;first_name = user_input.next( );

Page 19: #_ varible function

Java Option Panes

• Another useful class for accepting user input, and displaying results, is the JOptionPane class.

• This is located in the javax.swing library. • The JOptionPane class allows you to have i/o

boxes like this one:

Page 20: #_ varible function

Java Option Panes

• import javax.swing.JOptionPane;• This tells java that we want to use the

JOptionPane class, located in the javax.swing library.

Page 21: #_ varible function

What Is a Package?

• A package is a namespace that organizes a set of related classes and interfaces.

• The Java platform provides an enormous class library (a set of packages) suitable for use in your own applications. This library is known as the "Application Programming Interface", or "API" for short. Its packages represent the tasks most commonly associated with general-purpose programming.

Page 22: #_ varible function

How do I convert a String to an int data type in Java?

• int i = Integer.parseInt(myString);