Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

java programming elements

Rating
-
Sold
-
Pages
8
Uploaded on
16-06-2023
Written in
2022/2023

java programming notes

Institution
Course

Content preview

LEXICAL ELEMENTS OF JAVA LANGUAGE
a) Literals
A literal is a constant value that appears directly in the program. For example, 34, 1,000,000, and 5.0 are
literals. Java supports five types of literals:
 Integer literals
 Floating-point literals
 Character literals
 String literals
 Boolean literals
• Integer Literals: - An integer literal can be assigned to an integer variable as long as it can fit into the
variable. A compilation error would occur if the literal is too large for the variable to hold.
• Floating-Point Literals: - Floating-point literals are written with a decimal point. By default, a floating-
point literal is treated as a double type value. You can make a number a float by appending the letter f or F,
and make a number a double by appending the letter d or D. E.g. you can use 100.2f or 100.2F for a float
number.
• Scientific Notation: - Floating-point literals can also be specified in scientific notation, for example,
1.23456e+2(1.23456e2), is equivalent to 123.456, and 1.23456e-2 is equivalent to 0.0123456. E (or e)
represents an exponent and it can be either in lowercase or uppercase.

b) Identifiers
They are programmer-designed tokens, and used for naming classes, methods, variables, objects, labels,
packages and interfaces in a program. It follows the following rules and conventions
 They can have alphabets, digits, underscore and dollar sign characters.
 They must not begin with digit.
 Uppercase and Lowercase letters are distinct.
 They can be of any length.
 Names of methods and instance variables start with lowercase letter. When more than one word is
used in a name, the second word is marked with uppercase letter.
 All classes and interface start with uppercase letter.
 Identifiers that represent constant values use all uppercase letters and underscores between words.
 Should be unique within a given area (Scope).

c) Variables
Memory location used to store data during program execution and data in the memory may change with time.
An identifier used to refer to the memory location and as convention, use descriptive identifiers

Declaring Variables
• All variables must be declared before they are used i.e. Java is strongly typed language, meaning that data
type of the variable must be stated.
• General format for declaring a variable:
data_type identifier;
• Multiple variables of same type can be declared together.
• int and double variables initially are given a value of zero unless they are initialized to a value.
d) Data types
A category or set of data values. Many languages have a notion of data types and require a programmer to
specify what type of data is being manipulated.


Object Oriented Programming 1

, Java's primitive types
This are Java's built-in simple data types for numbers, text characters, and logic. Java has eight primitive types.
Types that are not primitive are called object types.
Java Data Types




primitive Objects
(reference)


Integral Boolean Floating point
String class array



float double
byte char short int long

Integer Primitive Type Variable
This are declared using the “int” keyword and are allocated 4 bytes. The range of values is from -2147483648
to 2147483647. Example: int aNum;

Floating Point Primitive Types
This are declared using “float” keyword and are allocated 4 bytes. The range of values is roughly ±1.4 x 10-38 to
±3.4 x 1038 to 7 significant digits.

Double Primitive Type Variable
There are declared using “double” keyword and allocated 8 bytes. The range of values is roughly ±4.9 x 10-308
to ±1.7 x 10308 to 15 significant digits.

More Integer Primitive Types
 Other primitive types: byte, short and long:
 For byte, from -128 to 127, inclusive (1 byte).
 For short, from -32768 to 32767, inclusive (2 bytes).
 For long, from -9223372036854775808 to 9223372036854775807, inclusive (8 bytes).

Characters
A char variable stores a single character from the Unicode character set. A character set is an ordered list of
characters, and each character corresponds to a unique number. Character literals are delimited by single quotes

A Boolean Primitive Type Variable
This are declared using “boolean” keyword. It can store either true or false values. Example boolean aFlag;

String
String’s are not primitive data types, but are Objects and can be declared in the same way as a primitive type
using the keyword: String. The String class has several methods that are useful for manipulating strings



Object Oriented Programming 2

Written for

Institution
Course

Document information

Uploaded on
June 16, 2023
Number of pages
8
Written in
2022/2023
Type
Class notes
Professor(s)
Mbuki
Contains
All classes

Subjects

$6.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
jokerinnit54

Get to know the seller

Seller avatar
jokerinnit54 KCA university
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
2 year
Number of followers
0
Documents
3
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions