0% found this document useful (0 votes)
9 views5 pages

About Java programs

Uploaded by

Cristine Versoza
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
9 views5 pages

About Java programs

Uploaded by

Cristine Versoza
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

About Java programs, it is very important to keep in mind the public static void main(String args[]) - java program

ava program processing


following points. starts from the main() method which is a mandatory part of every java
program..
Case Sensitivity - Java is case sensitive which means identifier Hello
and hello would have different meaning in Java. Coding Guidelines:
Class Names - For all class names the first letter should be in Upper 1. Your Java program should always end with the .java
Case. extension.
If several words are used to form a name of the class each inner words 2. Filenames should match the name of your public class. So, for
first letter should be in Upper Case. example, if the name of your public is Hello, you should save
it in a file called Hello.java.
Example class MyFirstJavaClass 3. You should write comments in your code explaining what a
Method Names - All method names should start with a Lower Case certain class does, or what a certain method do.
letter.
If several words are used to form the name of the method, then each JAVA COMMENTS
inner word's first letter should be in Upper Case.
Comments are notes written to a code for documentation purposes.
Example public void myMethodName() Those texts are not part of the program and do not affect the flow of
the program.
Program File Name - Name of the program file should exactly match Java supports three types of comments: C++- style single line
the class name. comment,C-style multiline comments and special javadoc comments.
When saving the file you should save it using the class name
(Remember java is case sensitive) and append '.java' to the end of the C++ Style Comments
name. (if the file name and the class name do not match your program C++ style comments starts with //. All the text after // are treated as
will not compile). comments. For example,
// This is a C++ style line comments
Example : Assume 'MyFirstJavaProgram' is the class name. Then the
file should be saved as 'MyFirstJavaProgram.java' C-Style Comments
C-style comments or also called multiline comments starts with a/*
and ends with a */. All text in between the two delimiter are treated as
comments. Unlike C++ style comments, it can span multiple lines. statements can be nested indefinitely. Any amount of white space is
For example, allowed. An example of a block is,
/* This is an example of a C style or public static void main (String [] args ) {
mulitiline System.out.println (“Hello”);
comments */ System.out.println (“world”);
}

Special Javadoc Comments Coding Guidelines:


1. In creating blocks, you can place the opening curly brace in
Special Javadoc comments are used for generating HTML line with the statement, like for example,
documentation for your Java programs. You can also crate javadoc
public static void main (String [] args ) {
comments by starting the line with /** and ending it with */. Like C-
style, it can also span lines. It can also contain certain tags to add or you can place the curly brace on the next line, like,
more information to your comments. For example,
public static void main (String [] args )
/** This is an example of special javadoc comments {
used for \n
2. You should indent the next statement after the start of a block,
generating HTML documentation. It uses tags
like: for example,
@author Florence Balagtas
@version 1.2 public static void main (String [] args ) {
*/ System.out.println (“Hello”);
System.out.println (“world”);
}
JAVA STATEMENTS AND BLOCKS

A statement is one or more lines of code terminated by a semicolon.


An example of a single statement is,
System.out.println(“Hello world”); JAVA IDENTIFIERS
All java components require names. Names used for classes,
A block is one or more statements bounded by an opening and variables and methods are called identifiers.
closing curly braces that groups the statements as one unit. Block
In java there are several points to remember about identifiers. They class goto protected try
are as follows: const If public void
continue implements return while
 All identifiers should begin with a letter (A to Z or a to z ), default import short
currency character ($) or an underscore ( _ ). do instanceof static
 After the first character identifiers can have any combination  Using Scanner to get an inputJava Scanner Class
of characters. The Java Scanner class is used to collect user input. Scanner is
part of the java.util package, so it can be imported without
 A key word cannot be used as an identifier.
downloading any external libraries. Scanner reads text from
 Most importantly identifiers are case sensitive. standard input and returns it to a program.

 Examples of legal identifiers: age, $salary, _value, __1_value In order to work with the Scanner class, you must first import
it into your code. There are two ways you can do this:
 Examples of illegal identifiers : 123abc, -salary
JAVA KEYWORDS 1. If you only need to work with the java.util.Scanner class,
you can import the Scanner class directly.
Keywords are predefined identifiers reserved by Java for a specific 2. If you are working with other modules in the java.util
purpose. You cannot use keywords as names for your variables, library, you may want to import the full library.
classes, methods…etc. here is a list of the Java keywords. The following is the code for each of the above methods:

abstract Double Int super import java.util.Scanner;


import java.util.*;
boolean Else Interface switch
break extends Long synchronized
byte False native this The first line of code imports the Scanner class. The second
byvalue Final New threadsafe line of code imports all the packages within the java.util library,
case finally Null throw including Scanner.
catch Float package transient
char For Private true Java User Input Syntax
You can collect user input using the Scanner class. The 4. Floating Point – float and double
Scanner class reads text that a user inserts into the console and sends Floating point types has double as default data type. Floating-
that text back to a program. Scanner is the primary method of point data types have the following ranges:
collecting Java user input.
Float Length Name or Type Range
After you import the Java Scanner class, you can start to use it 32 bits Float -231 to 231-1
to collect user input. Here is the syntax for the Java Scanner class: 64 bits Double -263 to 263-1
Scanner input = new Scanner(System.in);
int number = input.nextInt();
Decision Control Structures
Primitive data types
Decision control structures are Java statements that allow us to
The Java programming language defines eight primitive data select and execute specific blocks of code while skipping other
types. The following are, boolean (for logical), char (for textual), sections.
byte, short, int, long (integral), double and float (floating point).
if statement
1. Logical - boolean The if-statement specifies that a statement (or block of code)
A boolean data type represents two states: true and false. An will be executed if and only if a certain boolean statement is true.
example is,

boolean result = true; if-else statement


The if-else statement is used when we want to execute a
1. Textual – char certain statement if a condition is true and a different statement if the
char Letter=’a’; condition is false.
A character data type (char), represents a single Unicode
character. It must have its literal enclosed in single quotes(’ ’). if-else-if statement
3. Integral – byte, short, int & long The statement in the else-clause of an if-else block can be
another if-else structure. This cascading of structures allows us to
make more complex selections.
Integer Name or Type Range
8 bits Byte -27 to 27-1
16 bits Short -215 to 215-1
32 bits Int -231 to 231-1
64 bits Long -263 to 263-1
switch statement Nested loop means a loop statement inside another loop statement.
Another way to indicate a branch is through the switch That is why nested loops are also called as “loop inside loop“.
keyword. The switch construct allows branching on multiple
outcomes. Java is case sensitive which means identifier Hello and hello would
have different meaning in Java.
Repetition Control Structures For all class names the first letter should be in Upper Case. While all
Repetition control structures are Java statements that allow us method names should start with a Lower Case letter.
to execute specific blocks of code a number of times. There are three Comments are notes written to a code for documentation purposes.
types of repetition control structures, the while, do-while and for Those texts are not part of the program and do not affect the
loops. flow of the program.
A variable is an item of data used to store state of objects.
while loop Operator precedence defines the compiler’s order of evaluation of
The while loop is a statement or block of statements that is operators so as to come up with an unambiguous result.
repeated as long as some condition is satisfied. Decision control structures are Java statements that allow us to select
and execute specific blocks of code while skipping other
do-while loop sections.
The do-while loop is similar to the while-loop. The statements Repetition control structures are Java statements that allow us to
inside a do-while loop are executed several times as long as the execute specific blocks of code a number of times.
condition is satisfied.

for loop
The for loop, like the previous loops, allows execution of the
same code a number of times.

InitializationExpression -initializes the loop variable.


LoopCondition - compares the loop variable to some limit
value.
StepExpression - updates the loop variable.

Nested Loops

You might also like