0% found this document useful (0 votes)
4 views9 pages

4 - Java Lexical Issues

The document provides an overview of Java programming concepts, including the anatomy of a class, lexical issues such as whitespace, identifiers, literals, comments, separators, and keywords. It includes a simple program example with explanations of each line of code and details on Java's reserved keywords. The document emphasizes the importance of understanding these foundational elements for effective Java programming.

Uploaded by

1108lostman
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

4 - Java Lexical Issues

The document provides an overview of Java programming concepts, including the anatomy of a class, lexical issues such as whitespace, identifiers, literals, comments, separators, and keywords. It includes a simple program example with explanations of each line of code and details on Java's reserved keywords. The document emphasizes the importance of understanding these foundational elements for effective Java programming.

Uploaded by

1108lostman
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

• Simple program

• Anatomy of Class
• Lexical Issues
o White space
o identifiers
o Literals
o Comments
o Separators
o Keyword
• Example
Simple Program

Try to guess what each line of code is doing

(DO NOT COMPILE FOLLOWING PROGRAM)

int size =27;


String name = “Fido”;
Dog myDog = new Dog(name,size);
int x = size -5;
if (x <15) [Link](8);

while(x >3){
[Link]();
}

int[] numList = {2,4,6,8};


[Link](“Hello Java”);
[Link](“Dog : “ + name);
String num =”8”;
int z = [Link](num);

try
{
x = 10/0;
}
catch(Exception ex)
{
[Link](“divide by zero”);
}
int size =27; Declare an integer variable & initialize
String name = “Java”; Declare a String variable & initialize
Dog myDog = new Dog(name,size); Declare new Dog variable named “myDog“
int x = size -5; Subtract 5 from size variable
if (x <15) [Link](8); If x < 15 , tell the dog to bark 8 times

while(x >3){ Keep looping as long as x > 3


[Link](); Tell the dog to play
} End of loop

int[] numList = {2,4,6,8}; Declare an integer array & initialize


[Link](“Hello ”); print out “Hello”
[Link](name); Print out “Java”
String num =”8”; Declare string variable & initialize with “8”
int z = [Link](num); Convert String to Integer

try Try to do something that isn’t guaranteed to


{ work
x = 10/0; Divide by zero error will be generated
} Complete try bracket
catch(Exception ex) Find out thing if you tried didn’t work
{
[Link](“divide by zero”); Print error message
}
Looks like everything in the {} is what to do
If try didn’t work
Anatomy of Class
Lexical Issues

• Java programs are a collection of whitespace, identifiers, comments, literals,


operators, separators, and keywords.

Whitespace

• In Java, you do not need to follow any special indentation rules. For example, the
java program could have been written all on one line or in any other strange way
you felt like typing it, as long as there was at least one whitespace character
between each token that was not already delineated by an operator or separator.
• In Java, whitespace is a space, tab, or newline.

Identifiers

• Identifiers are used for class names, method names, and variable names.
• An identifier may be any descriptive sequence of uppercase and lowercase letters,
numbers, or the underscore and dollar-sign characters. They must not begin with
a number, lest they be confused with a numeric literal.
• Again, Java is case-sensitive, so VALUE is a different identifier than Value. Some
examples of valid identifiers are:

Valid : I, hello, $num , I_am_Variable


Invalid: 10i ,hello-java, And/Or

Literals

• A constant value in Java is created by using a literal representation of it. For


example,

100 : Integer literals


12.25 : float literals
‘J’ : character literals
“Hello Java” : String literals
0x10 : hex literals
010 : Octal literals
Comments

• As mentioned, there are three types of comments defined by Java.

1. Single-line: / / I am single line comment


2. Multiline: /* I am multiple line comment */
3. Documentation comment: This type of comment is used to produce an HTML file
that documents your program. The documentation comment begins with a /**
and ends with a */.

Separators

In Java, there are a few characters that are used as separators.


Symbol Name Purpose
() Parentheses Used to contain lists of parameters in method definition and
invocation. Also used for defining precedence in expressions,
containing expressions in control statements, and surrounding
cast types.
{} Braces Used to contain the values of automatically Initialized arrays.
Also used to define a block of code, for classes, methods, and
local scopes.
[] Brackets Used to declare array types. Also used when dereferencing
array values.
; Semicolon Terminates statements.
, Comma Separates consecutive identifiers in a variable
. Period Used to separate package names from subpackages and
classes. Also used to separate a variable or method from a
reference variable.
The Java Keywords

There are 49 reserved keywords currently defined in the Java language.


These keywords cannot be used as names for a variable, class, or method.

Data Type: boolean, byte, char, double, float, int, long, short

Control: break, case, continue, default, do, else, for, goto, if, return, switch, while.

Exception: catch, throw, throws, try, finally.

Class: abstract, class, extends, implements, import, interface, native, strictfp, super, this,
new, package

Access Control: public, private, protected,

Others : assert, const, final, instanceof, static, synchronized, transient, void, volatile

The keywords const and goto are reserved but not used.

Java reserves the following: true, false, and null


Example

You might also like