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

Topic 3_Elements of Java Programming (1)

The document outlines the fundamental elements of Java programming, including character sets, keywords, variables, literals, constants, data types, escape sequences, input/output methods, and comments. It details the rules for naming variables, types of variables based on scope, and provides examples of different data types and their characteristics. Additionally, it explains how to use the Scanner class for user input and the importance of comments in code for clarity and debugging.

Uploaded by

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

Topic 3_Elements of Java Programming (1)

The document outlines the fundamental elements of Java programming, including character sets, keywords, variables, literals, constants, data types, escape sequences, input/output methods, and comments. It details the rules for naming variables, types of variables based on scope, and provides examples of different data types and their characteristics. Additionally, it explains how to use the Scanner class for user input and the importance of comments in code for clarity and debugging.

Uploaded by

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

ELEMENTS OF JAVA PROGRAMMING

1. Alphabets
1. CHARACTER SET

A character set is a
Java accepts both lowercase and uppercase alphabets as variables and methods.
set of alphabets,
letters and some
2. Digits
special characters
that are valid in Java
language.
3. Special Characters

Special Characters in Java Programming


, < > . -
( ) ; $ :
’ & { } “
^ ! * / |
_ \ ~ +

4. White space Characters


Blank space, newline, horizontal tab, carriage return and form feed.
2. JAVA KEYWORDS

Keywords are predefined, reserved words used in programming that have special
meanings to the compiler.

As Java is a case sensitive language, all keywords must be written in lowercase.


There are only 48 keywords available in Java. Here is a list of all keywords allowed
in ANSI Java.

Java Keywords
abstract boolean break private do synchronized try
case catch char short final transient package

const continue default switch goto while return


double else extends throws instanceof new super
finally float for volatile native public throw
if implements import byte protected strictfp void
int interface long class static this
3. JAVA VARIABLES

In programming, a variable is used to store and manipulate data or values in


programs. A variable is the name that refers to a memory location where some data
value is stored.
To indicate the storage area, each variable should be given a unique name (identifier).
Variable names are just the symbolic representation of a memory location.
Rules for naming a variable
For example:
• The name of a variable needs to be meaningful, short, and without
any embedded space or symbol, such as ?, !, #, @, %, {}, [], :, ;, ”,
//Declare the variable
and /.
Int num1;
• A variable name must be unique.
//Assign value
• A variable name must begin with a letter, which can be followed by a
Num1 = 5;
sequence of letters or digits (0 to 9), ‘$’, or ‘_’.
OR • A variable name should not consist of a keywords.
int score = 55;
NB. A variable name is case sensitive.
TYPES OF JAVA VARIABLES

The area or the region of a program where a variable can be accessed is known as
variable scope. The various types of variables on the basis of the variable scope.

1. Class Variable: Are declared inside a class before their use. Class variables are
accessible within a class and its objects.

2. Instance Variable: Are declared inside a class and are created when the class is
instantiated. Objects are given different values to instance variables as per
requirement.

3. Local Variable: Are declared inside a method. Their scope is within the block of code
in which they are defined, and are not accessible outside the method.

4. Static Variables: Are allocated memory only once but are globally accessible to all
instance of a class. Therefore, when an instance of a class is destroyed, the static
variable is not destroyed and is available to other instance of that class.
4. JAVA LITERALS

Literals are the values to be stored in variables and constants. A literal contains a
sequence of characters, such as digits, alphabets, or any other symbol that
represents the value to be stored.

Types of Literals
1. Integers Literals :
Are numeric type values with whole parts.
2. Floating-point Literals
Are numeric values with fractional parts.
3. Character
Are enclosed in single quotation marks.
4. String Literals
Are enclosed in double quotation marks.
5. Boolean Literals
Are the literals having value, true or false.
JAVA LITERALS
For example:
An integer is a numeric literal(associated with
• decimal (base 10): 0, -9, 22 etc
numbers) without any fractional or exponential • octal (base 8): 021, 077, 033 etc
part. There are three types of integer literals in • Hexadecimal (base 16): 0x7f,
Java programming: 0x2a, 0x521 etc

A floating-point literal is a numeric literal that


has either a fractional form or an exponent form. For example: -2.0

A character literal is created by enclosing a single For example: ‘a’


character inside single quotation marks.

A string literal is a sequence of characters For example: ‘good’


enclosed in double-quote marks.

A boolean literal have values, true or false. For example, x = false


5. JAVA CONSTANTS

If you want to define a variable whose value cannot be changed, you can use the
const keyword. This will create a constant.

For example,

const double PI = 3.14;

Notice, we have added keyword const.


Here, PI is a symbolic constant; its value cannot be changed.

const double PI = 3.14;


PI = 2.9; //Error
6. JAVA DATA TYPES

The data stored in memory of the computer can be of many types. For example, a
person’s age is stored as a numeric value and an address is stored as alphanumeric
characters. Data types are used to define the operations possible on variables and the
storage method.

The various data types in Java are:


▪ Primitive or the simple data types
▪ Abstract or the derived data types

Primitive Data Types: These are the built-in data types. There are eight primitive
data types in Java, which are further grouped in the following categories.
▪ Integer type: Byte, short, int and long
▪ Floating point type: float and double
▪ Boolean type
▪ Character type
Default
Group Data Type Size (bytes) Range Value

byte One byte -27 to 27-1 (signed) 0

short Two byte -215 to 215-1 0

Integer int Four byte -231 to 231-1 0

long Eight byte -263 to 263-1 0

float Four byte 3.4e-038 to 3.47e+038 0.0

double Eight byte 1.7e-308 to 1.77e+308 0.0


Floating point
boolean One bit true or false false
Boolean
Character char Two byte a single character null

Abstract Data Types: These are derived from the primitive data types and have
more functions than primitive data types. For example, String is an abstract data type
that can store letters, digits and other characters, such as /, (), :, ;, $, and #.
7. Escape Sequence Escape Character
Sequence
Escape Sequence are characters that cannot \b Backspace
be typed.
\f Form feed
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\’ Single quotation
mark
\” Double quotation
mark
\? Question mark
\0 Null character
8. JAVA OUTPUT
Java programming language provides a set of built-in methods to output the data on the
computer screen.

System.out.println(); or
System.out.print(); or
System.out.printf();

Here,
▪ System is a class
▪ Out is a public static field: It accepts output data.

Difference between println(), print() and printf()


▪ print() - It prints string inside the quotes.
▪ println() - It prints string inside the quotes similar like print() method. Then the
cursor moves to the beginning of the next line.
▪ printf() - It provides string formatting (similar to printf in C/C++ programming).
9. JAVA INPUT
Java provides different ways to get input from the user using the object of Scanner class.

In order to use the object of Scanner, we need to import java.util.Scanner package

import java.util.Scanner;

Then, we need to create an object of the Scanner class. We can use the object to take
input from the user.

// create an object of Scanner


Scanner input = new Scanner(System.in);

// take input from the user


int number = input.nextInt();

We will create an object named input of the Scanner class. We then call the nextInt()
method of the Scanner class to get an integer input from the user.
Similarly, we can use nextLong(), nextFloat(), nextDouble(), and next() methods to get
long, float, double, and string input respectively from the user.
10. JAVA COMMENTS

In programming, comments are hints that a programmer can add to make their
code easier to read and understand.

For example
Use of Comments in Java
// declare and initialize two variables 1. Make Code Easier to Understand
int a =1; If we write comments on our code, it will be easier to
int b = 3; understand the code in the future.

Otherwise you will end up spending a lot of time looking


// print the output at our own code and trying to understand it.
System.out.println("This is output");
2. Using Comments for debugging
While debugging there might be situations where
we don't want some part of the code.
Types of Comments For example

// "Hello, World!" program example


There are two ways to add comments in Java:

1. // - Single Line Comment class Main {


public static void main(String[] args) {
2. /*...*/ - Multi-line Comment

Single-line Comments in Java // prints "Hello, World!"


In Java, a single line comment starts with //.
System.out.println("Hello, World!");
It starts and ends in the same line.
}
Multi-line Comments in Java }
In Java programming, there is another type
of comment that allows us to comment on /* This is an example of multi-line
multiple lines at once, they are multi-line comment.
comments.
* The program prints "Hello, World!"
To write multi-line comments, we use the to the standard output.
/*....*/ symbol. */

You might also like