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

Java Programming Feb20

The document summarizes the key elements of a basic Java program structure including: 1. Every Java program line must end with a semicolon, keywords are case sensitive, and the main method is where code execution begins. 2. The basic structure includes a package name, class declaration matching the file name, and a main method defined as public static void along with curly braces. 3. Print statements like println() are used to output text, and comments are for documentation and are ignored by the compiler.

Uploaded by

milliongate1122
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Java Programming Feb20

The document summarizes the key elements of a basic Java program structure including: 1. Every Java program line must end with a semicolon, keywords are case sensitive, and the main method is where code execution begins. 2. The basic structure includes a package name, class declaration matching the file name, and a main method defined as public static void along with curly braces. 3. Print statements like println() are used to output text, and comments are for documentation and are ignored by the compiler.

Uploaded by

milliongate1122
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Java Programming

Notes:
 Every keyword written in the Java Program is Case Sensitive, which means that the
capital letters are different from the corresponding small letter.

In the println() lines, System must start with a capital S. Using lowercase s makes string different and therefore results in an error.
 Every line in the java program must end with a semicolon. Exceptions to this are on
method and class declarations, closing curly brackets ‘}’, comments, etc..
______________________________________________________________________________
______
Java Program Structure (NetBeans)
The structure of a Java program written in NetBeans is presented when you create a new
project/file:

It consists of the package line, the class declaration, and the main method. The comments
which are shown in blue are optional and can be removed from the code. Let’s dive deeper into
what these lines do:

Package
The package is the reference of the program to what project the file belongs to. The
syntax are as follows:

 The project name MUST be the same as what was written in the program (newfile).
 The project name (newfile) should be written in lowercase letters. If your project name
contains a capital letter, the code writes the project name in all lowercase.
 package must be written in lowercase letters.
 The line must end in a semicolon ‘;’.

By: ProgNgCavs
Class Declaration
The class declaration line creates the class the main method of the project should be in.
The syntax are as follows:

 Keywords public and class must be lowercase.


 The class name must be the same as the project name.
 The class name always starts with a capital letter. If your project name starts with a
lowercase letter, the code writes the class name with the first letter capital.
 Curly braces ‘{}’ are required.

Main Method
The main method is the method the Java compiler starts executing code on. The code on
other methods won’t run unless the main method calls the specified method. It consists of the
keywords public static void, the identifier main, and the parameters (String[] args). The syntax
are as follows:

 The public keyword indicates that the method can be accessed by other classes in the
project.
 The static keyword indicates that the method can be called by the compiler without
creating an object instance of the method.
 The void keyword indicates that there is no output of the method, meaning that the return
line is not necessary.
 All keywords stated must be in lowercase.
 The identifier main() can be used to call the method from other methods.
 main() must be in lowercase.
 String[] is the data type of the args parameter, which specifically is a String Array. If you
put any string values inside the parentheses when you call the method main(), it would
then store the strings in the String Array parameter args.
 String[] is a must in the main method. args may be replaced by another name but for
simplicity, you may leave it as it is.
 Curly Braces ‘{}’ is required.
 Inside the curly braces is the main code of the program. As stated before, this block of
code is the first lines of code the compiler reads and executes without having to manually
call the method.

By: ProgNgCavs
Print Lines
There are two basic lines we can use to get an output from a program, the println() line
and the print() line.

These lines print the string or variable enclosed in the parentheses. The difference
between the two print methods is that after printing, println() creates a new line below the printed
output, while print() does not. You can visualize this with the enter (↵) key. println() presses the
enter key after printing and print() does not.

 System must start with a capital letter.


 You cannot remove “System.out.” from the line.
 out, println, and print must be in all lowercase.

Comments
Comments are the lines the compiler ignores and therefore will not be executed. It can be
used to document the code, to help other programmers understand the flow of the program, and
sometimes it can be used to debug the code by commenting lines to find the cause of the error
(NetBeans already checks the code for errors, therefore commenting lines to debug is not
necessary). Comments are enclosed in a backslash-asterisk (/*, */) form.

 Removing any of the comment tags (/*, */) would consider every line above or below the
commented line a comment.

Java Operators

By: ProgNgCavs
Operators are an indicator of an operation a program performs on variables, integers, or
even conditions. There are 3 types of operators namely arithmetic, relational, and logical. There
are also 3 groups of operators which are Unary, Binary, and Ternary Operators.

Unary Operators
Unary Operators use only one operand. Examples of unary operators are the bitwise
complement (~), which reverses 0’s and 1’s of the binary representation of the number, negation
(-), increment (++), and decrement (--).

Binary Operators
Binary operators use two operands. Most of the operators in Java are binary, especially
the arithmetic operators.

Ternary Operators
Ternary operators use three operands. The only Ternary Operator in Java is the
conditional operator ‘? :’.

Arithmetic Operators
Arithmetic Operators are mathematical symbols that perform the corresponding operation
on any given integer. The operators are performed in a specific order like
PEMDAS/GEMDAS/BODMAS.

Operator and Symbol Method Example


Addition (+) Adds the values together a=6+2 a=3
Subtraction (-) Subtracts the values together a=6–2 a=4
Multiplication (*) Multiplies the values together a=6*2 a = 12
Division (/) Divides the values together a = 6/2 a=3
(divisor goes first)
Modulo Division (%) Gets the remainder of the values a=6%5 a=1
(divisor goes first)
Exponentiation (^) Raises the value to the power of a = 6^2 a = 36
another value
(base goes first)
Increment (++) Increments the value by 1 a = 6++ a=7
Decrement (--) Decrements the value by 1 a = 6-- a=5

Relational Operators
Relational Operators check the relationship between the two values. The output of the operation
is Boolean only.

Operator and Symbol Method Example


Equal to (==) True if two values are equal 24 == 24 (True)
Greater than (>) True if the first value is greater 26 > 34 (False)
than the second.
Less than (<) True if the first value is less than 8 < 10 (True)
the second.
Greater than or equal to (>=) True if the first value is greater 10 >= 4 (True)
than or equal to the second.
Less than or equal to (<=) True if the first value is less than 6 <= 6 (True)

By: ProgNgCavs
or equal to the second.
Not equal to (!=) True if the values are not equal 4 != 4 (False)

Logical Operators
Logical Operators check the conditions relationship. Logical Operators give a Boolean
output.

Operator and Symbol Method Truth Table


AND (&) and Short Circuit True if both conditions are C1 C2 Output
AND (&&) True T T T
T F F
F T F
F F F
OR (|) and Short Circuit OR True if at least one condition C1 C2 Output
(||) is True T T T
T F T
F T T
F F F
NOT (!) True if condition is False C1 Output
T F
F T
XOR (^) True if only one condition is C1 C2 Output
True T T F
T F T
F T T
F F F

By: ProgNgCavs
Keywords
Keywords are words derived from several methods in Java which are exclusive only to
that method, meaning you can’t use it as an identifier.

List of Keywords:

Source: https://www.shiksha.com/online-courses/articles/keywords-in-java/

 Capitalizing the keyword makes it no longer a keyword, therefore it can be used as an


identifier.

Identifiers
Identifiers are names given by programmers to various methods, variables, and classes.
There are many rules in creating identifiers including:
1. The first character must be a Unicode letter, an underscore (_), or a dollar sign ($).
2. The following characters can be a letter, number, or an underscore (_).
3. Special characters are not allowed to be used in identifiers, excluding the underscore (_)
and the dollar sign ($).
4. Keywords are not allowed to be used as an identifier, but changing the capitalization of
the keyword is a bypass to the rule.
5. Optional: You can use camelCase format in case it is made up of several words.
6. Try to make the identifier descriptive as much as possible.

Data Types
Variables are storage of data in a program. Each variable a programmer creates has a use
in the program, whether it is in computing, or storing someone’s name. Data types are a way to
sort the variables that have different properties. There are two main types of data types, namely
primitive and reference data types. Primitive data types only store a single type of data.
Reference data types store a combination of these data types, which is still sorted in some way.

By: ProgNgCavs
There are eight data types used in Java:
Data Type What data? Example
Boolean True, False True
char Any Unicode data type ‘A’
(Must be enclosed in single quotes ‘’)
float 32-bit +/- Decimal 1023.14
double 64-bit +/- Decimal 91842387.274
byte 8-bit +/- number 25
short 16-bit +/- integer 118
int 32-bit +/- integer 724
long 64-bit +/- integer 10927
String Set of chars. “Hello World!”
(Must be enclosed in double quotes “”)

Variables
As stated earlier, variables store data so the program can compute or modify the data and
give the output. The syntax of variable declaration are as follows:

First, we will state the data type of the variable. Second, we input our identifier or the
name of the variable. And lastly, we will give it a value (literal) by using an equal sign (don’t get
it confused with the Equals to Operator ‘==’).
 The value of the variable must match the data type of the variable.
 Follow the rules of naming identifiers.

Literals
Literal is the value of a variable, which belongs to a particular type. Just like keywords,
there are some literals that cannot be used as an identifier such as null, True, and False.

By: ProgNgCavs

You might also like