2 Java Basic Syntax
2 Java Basic Syntax
2
Syntax and Semantics
• The syntax rules of a language define how we can combine reserved
words, symbols, and identifiers
• The semantics of a program statement define what the statement
means
• Problem with program syntax = “error”
• Problem with program semantics = “bug”
3
The Java Language (cont’d)
• … is a high-level programming language
• … is very object oriented
• … is similar to C++ and C
• … typically compiled to Java bytecode
• … is often confused with the Java Platform, but these are two
different aspects of “Java”
4
Java Program Structure
• A Java program consists of:
• One or more classes
• A class contains one or more methods
• A method contains program statements
• We will explore these terms in detail
5
Java Program Structure
// comments about the class
public class ClassName
{
class h ead er
-the first line of code
in a Java class and it
defines the name of the
class b o d y class.
consists of the keyword "
class " and the name you give
to the class.
6
Java Program Structure
-m eth o d h ead er
// comments about the class
-d river co d e
public class ClassName
{
m eth o d b o d y
}
7
Hello World
// HelloWorld.java
public class HelloWorld
{
public static void main (String[] args)
{
System.out.println(“Hello World!”);
}
}
8
Hello World
// HelloWorld.java
public class HelloWorld
{
public static void main (String[] args)
{
System.out.println(“Hello World!”);
}
}
9
Hello World
// HelloWorld.java
public class HelloWorld
{
public static void main (String[] args)
{
System.out.println(“Hello World!”);
}
}
10
Hello World
// HelloWorld.java
public class HelloWorld
{
public static void main (String[] args)
{
System.out.println(“Hello World!”);
}
}
11
Compiling and Running
• Create the file HelloWorld.java in a text editor
• Compile:
• javac HelloWorld.java
• Run:
• java HelloWorld
• Output:
• Hello World!
12
Comments
• Three kinds of comments:
// a one-line comment
/* a multi-line
comment */
/** a javadoc comment */
13
Statements & Blocks
• A simple statement is a command terminated by a semi-colon:
BULSU CICT 14
Reserved Words and Identifiers
• Reserved words
• are specified by the language
• All Java reserved words are in the text
• Identifiers
• are specified by a programmer
• Maybe you: e.g. HelloWorld
• Maybe someone else: e.g. println
15
P a s c a lC a s e (U p p e r
Restrictions and Conventions C a m e l C a s e ):
In PascalCase, each word in the identifier
is capitalized, including the first one. It’s
often used in programming languages for
• Restriction naming classes, records, and other user-
• Identifiers can not start with a digit defined types. Ex:
CustomerService, UserProfile,
• Conventions an d LoginAccounts
• PascalCase/Title case for class
names: HelloWorld
• Uppercase for constants: MAX camelCase (Lower Camel Case):
camelCase is similar to PascalCase, but the first letter of the
• CamelCase first word is in lowercase. It’s commonly used for naming
• Methods/functions, variables variables and functions.
Ex: customerService, userProfile , and
loginAccounts
16
Java Identifiers
• All java components require names. Names used for classes, variables and
methods are called
• In java there are several points to remember about identifiers. They are as
follows:
• All identifiers should begin with a letter ( ), currency character ($) or an
underscore (-).
• After the first character identifiers can have any combination of characters.
• A CANNOT be used as an identifier.
• Most importantly identifiers are
BULSU CICT 17
Identifiers
BULSU CICT 18
Basic Syntax
• C a s e S e n s i t i v i t y - JAVA I S C A S E S E N S I T I V E w h i c h m e a n s
identifier Hello and hello would have different meaning in Java.
• Class Names - For all class names the first letter should be in Upper Case.
If several words are used to form a name of the class each inner words first
letter should be in Upper Case.
BULSU CICT 19
White Space Conventions
20
Strong Typing
• Java is a “strongly typed” language
• All variables and values have a specific type
• Type is known when the program is compiled…. before it is run
• So all variables and values must be declared with a type before being
used
21
Declaring Variables
The Java variable declaration creates a new variable with required properties. The
programming language requires four basic things to declare a variable in the program.
4
• Syntax:
Data_type variable_name = valu
e;
1 2 3
Examples:
int count1, int count 2;
int count = 0;
String studentName = “Raiqa
Reyes”;
char letterA = ‘A’;
22
Variables
• They are used to store data such as numbers and letters.
Example:
char letterA = ‘A’;
variable = expression or value;
double num1,num2;
String studentName;
boolean status;
status = true;
BULSU CICT 23
Assignment
• We use the = operator for variable assignment
• Initialization is a special case
• When a value is assigned, the old value is overwritten
• In Java, we use the final modifier to declare a variable constant
• final int MAX_HEIGHT = 6;
24
Coding Standards & Best Practices To Follow
1. Write as few lines as possible.
2. Use appropriate naming conventions.
3. Segment blocks of code in the same section into paragraphs.
4. Use indentation to marks the beginning and end of control structures.
Clearly specify the code between them.
5. Don’t use lengthy functions. Ideally, a single function should carry out
a single task.
6. Use the DRY (Don’t Repeat Yourself) principle. Automate repetitive tasks
whenever necessary. The same piece of code should not be repeated in the
script.
7. Avoid Deep Nesting. Too many nesting levels make code harder to read and
follow.
8. Capitalize special words and function names to distinguish them from
table and column names.
9. Avoid long lines. It is easier for humans to read blocks of lines that
are horizontally short and vertically long.
VALID OR NOT
_myvariable $variabletest B5
1Student
1$test_variable TestVariable
id_no_
a2 3x
!ValidResults
totalAmt_1 -averageGrade
10_discounted_price x1