Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Java Fundamentals

The document discusses Java programming fundamentals including identifying parts of a Java program, data types, variables, operators, and developing a simple Java program. It also examines a sample 'Hello World' Java program line-by-line and explains the purpose and meaning of each section.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Java Fundamentals

The document discusses Java programming fundamentals including identifying parts of a Java program, data types, variables, operators, and developing a simple Java program. It also examines a sample 'Hello World' Java program line-by-line and explains the purpose and meaning of each section.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Programming

Fundamentals
OBJECTIVES
At the end of the lesson, the student should be
able to:
• Identify the basic parts of a Java program
• Differentiate among Java literals, primitive
data types, variable types ,identifiers and
operators
• Develop a simple valid Java program using
the concepts learned in this chapter
DISSECTING MY FIRST JAVA
PROGRAM
1 public class Hello
2 {
3 /**
4 * My first Java program
5 */
6 public static void main( String[] args ){

1 //prints the string Hello world on


screen
2 System.out.println(“Hello world”);

1 }
2 }
DISSECTING MY FIRST JAVA PROGRAM
1 public class Hello
2 {
3 /**
4 * My first Java program
5 */
● indicates the name of the class which is Hello
● In Java, all code should be placed inside a class
declaration
● The class uses an access specifier public, which indicates
that our class in accessible to other classes from other
packages (packages are a collection of classes). We will
be covering packages and access specifiers later.
DISSECTING MY FIRST JAVA PROGRAM

1 public class Hello


2 {
3 /**
4 * My first Java program
5 */

• The next line which contains a curly brace { indicates the


start of a block.
• In this code, we placed the curly brace at the next line
after the class declaration, however, we can also place
this next to the first line of our code. So, we could
actually write our code as:
DISSECTING MY FIRST JAVA PROGRAM
1 public class Hello
2 {
3 /**
4 * My first Java program
5 */

● The next three lines indicates a Java comment.


● A comment
− something used to document a part of a code.
− It is not part of the program itself, but used for
documentation purposes.
− It is good programming practice to add comments to your
code.
DISSECTING MY FIRST JAVA PROGRAM
1 public class Hello
2 {
3 /**
4 * My first Java program
5 */
6 public static void main( String[] args ){

● indicates the name of one method in Hello which is the


main method.
● The main method is the starting point of a Java program.
● All programs except Applets written in Java start with the
main method.
● Make sure to follow the exact signature.
DISSECTING MY FIRST JAVA PROGRAM

1 public class Hello


2 {
3 /**
4 * My first Java program
5 */
6 public static void main( String[] args ){

1 //prints the string “Hello world” on


screen
● The next line is also a Java comment
DISSECTING MY FIRST JAVA PROGRAM

1 public class Hello


2 {
3 /**
4 * My first Java program
5 */
6 public static void main( String[] args ){

1 //prints the string “Hello world” on


screen
2 System.out.println(“Hello world”);

● The command System.out.println(), prints


the text enclosed by quotation on the
screen.
DISSECTING MY FIRST JAVA PROGRAM

1 public class Hello


2 {
3 /**
4 * My first Java program
5 */
6 public static void main( String[] args ){

1 //prints the string “Hello world” on


screen
2 System.out.println(“Hello world”);

1 }
2 }
● The last two lines which contains the two curly braces is
used to close the main method and class respectively.
CODING GUIDELINES
1. Your Java programs should always end with the
.java extension.

2. Filenames should match the name of your public


class. So for example, if the name of your public
class is Hello, you should save it in a file called
Hello.java.

3. You should write comments in your code


explaining what a certain class does, or what a
certain method do.
JAVA COMMENTS
• Comments
– These are notes written to a code for documentation
purposes.
– Those texts are not part of the program and does not
affect the flow of the program.

• 3 Types of comments in Java


– C++ Style Comments
– C Style Comments
– Special Javadoc Comments
Java Comments

• C++-Style Comments
–C++ Style comments starts with //
–All the text after // are treated as
comments
–For example:
// This is a C++ style
or single line comments
Java Comments
• C-Style Comments
– C-style comments or also called multiline
comments starts with a /* and ends with a */.
– All text in between the two delimeters are
treated as comments.
– Unlike C++ style comments, it can span multiple
lines.
– For example:
/* this is an exmaple of a
C style or multiline comments
*/
Java Comments
• Special Javadoc Comments
– Special Javadoc comments are used for generating an HTML
documentation for your Java programs.
– You can create javadoc comments by starting the line with /**
and ending it with */.
– Like C-style comments, it can also span lines.
– It can also contain certain tags to add more information to your
comments.
– For example:
/** This is an example of special java doc
comments used for \n generating an
html
documentation. It uses tags like:
@author Florence Balagtas
@version 1.2
*/
JAVA STATEMENTS

• Statement
–one or more lines of code
terminated by a semicolon.
–Example:
System.out.println(“Hell
o world”);
JAVA BLOCKS
• Block
– is one or more statements bounded by an opening and
closing curly braces that groups the statements as one
unit.
– Block statements can be nested indefinitely.
– Any amount of white space is allowed.
– Example:
public static void main( String[]
args ){
System.out.println("Hello");
System.out.println("world”);
}
Java Statements and Blocks
Coding Guidelines
1. In creating blocks, you can place the opening curly
brace in line with the statement. For example:
public static void main( String[] args
){
or you can place the curly brace on the next line,
like,
public static void main( String[] args
)
{
JAVA STATEMENTS AND BLOCKS
CODING GUIDELINES
2. You should indent the next statements after
the start of a block. For example:
public static void main( String[]
args ){
System.out.println("Hello");
System.out.println("world");
}
JAVA IDENTIFIERS

• Identifiers
– are tokens that represent names of variables,
methods, classes, etc.
– Examples of identifiers are: Hello, main,
System, out.

• Java identifiers are case-sensitive.


– This means that the identifier Hello is not the
same as hello.
JAVA IDENTIFIERS
• Identifiers must begin with either a letter,
an underscore “_”, or a dollar sign “$”.
Letters may be lower or upper case.
Subsequent characters may use numbers 0
to 9.

• Identifiers cannot use Java keywords like


class, public, void, etc. We will discuss more
about Java keywords later.
JAVA IDENTIFIERS
CODING GUIDELINES
1. For names of classes, capitalize the first letter
of the class name. For example,
ThisIsAnExampleOfClassName

2. For names of methods and variables, the first


letter of the word should start with a small
letter. For example,
thisIsAnExampleOfMethodName
Java Identifiers
Coding Guidelines
3. In case of multi-word identifiers, use capital letters to
indicate the start of the word except the first word.
For example,
charArray, fileNumber, ClassName.

4. Avoid using underscores at the start of the identifier


such as _read or _write.
Java Keywords

• Keywords are predefined identifiers


reserved by Java for a specific
purpose.
• You cannot use keywords as names
for your variables, classes, methods ...
etc.
• The next line contains the list of the
Java Keywords.
Java Keywords
Java Literals

• Literals are tokens that do not change or are


constant.
• The different types of literals in Java are:
– Integer Literals
– Floating-Point Literals
– Boolean Literals
– Character Literals
– String Literals
Java Literals: Integer
• Integer literals come in different formats:
– decimal (base 10)
– hexadecimal (base 16)
– octal (base 8).
Java Literals: Integer
• Special Notations in using integer literals in our
programs:
– Decimal
• No special notation
• example: 12
– Hexadecimal
• Precede by 0x or 0X
• example: 0xC
– Octal
• Precede by 0
• example: 014
Java Literals: Floating Point

• Represents decimals with fractional


parts
– Example: 3.1416

• Can be expressed in standard or


scientific notation
– Example: 583.45 (standard), 5.8345e2
(scientific)
Java Literals: Boolean

• Boolean literals have only two values,


true or false.
Java Literals: Character

• Character Literals represent single Unicode


characters.

• Unicode character
– a 16-bit character set that replaces the 8-bit
ASCII character set.
– Unicode allows the inclusion of symbols and
special characters from other languages.
Java Literals: Character
• To use a character literal, enclose the character
in single quote delimiters.

• For example
– the letter a, is represented as ‘a’.
– special characters such as a newline character, a
backslash is used followed by the character code. For
example, ‘\n’ for the newline character, ‘\r’ for the
carriage return, ‘\b’ for backspace.
Java Literals: String

• String literals represent multiple


characters and are enclosed by double
quotes.

• An example of a string literal is, “Hello


World”.
Primitive Data Types
• The Java programming language defines eight
primitive data types.
– boolean (for logical)
– char (for textual)
– byte
– short
– int
– long (integral)
– double
– float (floating point).
Primitive Data Types: Logical-
boolean
• A boolean data type represents two states:
true and false.

• An example is,
boolean result = true;

• The example shown above, declares a


variable named result as boolean type and
assigns it a value of true.
Primitive Data Types: Textual-char
• A character data type (char), represents a single
Unicode character.
• It must have its literal enclosed in single quotes(’
’).
• For example,
‘a’ //The letter a
‘\t’ //A tab
• To represent special characters like ' (single
quotes) or " (double quotes), use the escape
character \. For example,
'\'' //for single quotes
'\"' //for double quotes
Primitive Data Types: Textual-char
• Although, String is not a primitive data type (it is
a Class), we will just introduce String in this
section.

• A String represents a data type that contains


multiple characters. It is not a primitive data
type, it is a class.

• It has its literal enclosed in double quotes(“”).

• For example,
String message=“Hello world!”;
Primitive Data Types: Integral – byte,
short, int & long
• Integral data types in Java uses three forms –
decimal, octal or hexadecimal.
• Examples are,
2 //The decimal value 2
077 //The leading 0 indicates an octal value
0xBACC //The leading 0x indicates a hex value
• Integral types has int as default data type.
• You can define its long value by appending the
letter l or L.
• For example:
10L
Primitive Data Types: Integral – byte,
short, int & long
• Integral data type have the
following ranges:
Primitive Data Types: Integral – byte,
short, int & long
• Coding Guidelines:
– In defining a long value, a lowercase L is not
recommended because it is hard to distinguish
from the digit 1.

You might also like