[go: up one dir, main page]

0% found this document useful (0 votes)
42 views18 pages

CC102 Week-5 Lesson-Programming Basics

The document discusses programming basics in Java, including data types, variables, and keywords. It covers the different primitive data types in Java like boolean, byte, char, short, int, long, float, and double. It also explains variable scope and types, and keywords like class, public, static, and void. The document provides examples of Java code for variables, data types, and a simple program to add two numbers.

Uploaded by

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

CC102 Week-5 Lesson-Programming Basics

The document discusses programming basics in Java, including data types, variables, and keywords. It covers the different primitive data types in Java like boolean, byte, char, short, int, long, float, and double. It also explains variable scope and types, and keywords like class, public, static, and void. The document provides examples of Java code for variables, data types, and a simple program to add two numbers.

Uploaded by

Tred Moon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

WEEK 5

PROGRAMMING BASICS
CC102 – PROGRAMMING 1
• Discuss the the different data types used in java
programming;
• Understand the variable declaration, identifiers
and keywords;
• Recognize the different Java keywords
categorically.

2
Parameters used in First Java Program
• class keyword is used to declare a class in java.
• public keyword is an access modifier which represents visibility.
• static is a keyword. If we declare any method as static, it is known as the static
method.
• void is the return type of the method. main represents the starting point of
the program.
• String[] args is used for command line argument.
• System.out.println() is used to print statement.
3
Basic Syntax
• Case Sensitivity − Java is case sensitive, which means 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 word's first letter should be in Upper Case.
• Example: class MyFirstJavaClass
• Method Names − All method names should start with a Lower Case letter. If several words
are used to form the name of the method, then each inner word's first letter should be in
Upper Case.
• Example: public void myMethodName()
• Program File Name − Name of the program file should exactly match the class name.
4
Java Keywords
• Java keywords are also known as reserved words. Keywords are particular
words which acts as a key to a code. These are predefined words by Java so it
cannot be used as a variable or object name.

5
Java Identifiers
• All Java components require names. Names used for classes, variables, and
methods are called identifiers.
• All identifiers should begin with a letter (A to Z or a to z), currency character ($)
or an underscore (_).
• After the first character, identifiers can have any combination of characters.
• A key word cannot be used as an identifier.
• Most importantly, identifiers are case sensitive.
• Examples of legal identifiers: age, $salary, _value, __1_value.
• Examples of illegal identifiers: 123abc, -salary.
6
Java Variables
• A variable is a container which holds the value while the java program is
executed. A variable is assigned with a datatype.
• Variable is a name of memory location. There are three types of variables in
java: local, instance and static.

7
Java Variables
• A variable is a container which holds the value while the java program is
executed. A variable is assigned with a datatype.
• Variable is a name of memory location. There are three types of variables in
java: local, instance and static.

 Local Variables.
 Instance Variable
 Static variable

8
Java Variables

 Local Variables. Local variables are declared in methods, constructors,


or A variable declared inside the body of the method is called local
variable.
 Instance Variable. A variable declared inside the class but outside the
body of the method.
 Static variable. A variable which is declared as static and it cannot be
local.

9
Java Variables
• Example to understand the types of variables in java

class A{  
int data=50;//instance variable  
static int m=100;//static variable  

void method(){  
int n=90;//local variable  
}  
}//end of class  
10
Java Variables
• Java Variable Example: Add Two Numbers
class Simple{  
public static void main(String[] args){  
int a=10;  
int b=10;  
int c=a+b;  
System.out.println(c);  
}}
11
Data Types in Java
• Data types specify the different sizes and values that can be stored in
the variable. There are two types of data types in Java:

• Primitive data types: The primitive data types include boolean,


char, byte, short, int, long, float and double.
• Non-primitive data types: The non-primitive data types include
Classes, Interfaces, and Arrays.

12
Java Primitive Data Types
• are the building blocks of data manipulation. These are the most
basic data types available in Java language.
• There are 8 types of primitive data types:
1. boolean data type 5. int data type
2. byte data type 6. long data type
3. char data type 7. float data type
4. short data type 8. double data type

13
Data Types in Java
Boolean Data Type
The Boolean data type is used to store only two possible values: true and false. This data
type is used for simple flags that track true/false conditions.
Example: Boolean one = false

Byte Data Type


The byte data type is an example of primitive data type. It isan 8-bit signed two's
complement integer. Its value-range lies between -128 to 127 (inclusive). Its minimum
value is -128 and maximum value is 127. Its default value is 0.
Example: byte a = 10, byte b = -20

14
Data Types in Java
Short Data Type
The short data type is a 16-bit signed two's complement integer.
Example: short s = 10000, short r = -5000

Int Data Type


The int data type is a 32-bit signed two's complement integer.
Example: int a = 100000, int b = -200000

15
Data Types in Java
Long Data Type
The long data type is a 64-bit two's complement integer.
Example: long a = 100000L, long b = -200000L

Float Data Type


The float data type is a single-precision 32-bit IEEE 754 floating point.
Example: float f1 = 234.5f

16
References:
• TutorialsPoint.,( 2019). Learn Java Programming Retrieved from:
https://www.tutorialspoint.com/java.
• JavaTPoint., (2019). Java Tutorial. Retrieved from:
https://www.javatpoint.com/java-tutorial

17
18

You might also like