0% found this document useful (0 votes)
6 views13 pages

Lecture 1 - Java Boilerplate, Variables Data Types- NUST

This lecture covers the basics of Java programming, including boilerplate code, variable declaration, and data types. It emphasizes the importance of naming conventions for classes and variables, as well as the structure of the main method. Additionally, it outlines the eight primitive data types available in Java and their characteristics.
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)
6 views13 pages

Lecture 1 - Java Boilerplate, Variables Data Types- NUST

This lecture covers the basics of Java programming, including boilerplate code, variable declaration, and data types. It emphasizes the importance of naming conventions for classes and variables, as well as the structure of the main method. Additionally, it outlines the eight primitive data types available in Java and their characteristics.
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
You are on page 1/ 13

Lecture 1: Java

Boilerplate, Variables &


Data Types
Agenda
1. Java boilerplate code
2. Introduction to Variables in Java
3. Demo of boilerplate code and variables
4. How to read in an integer
Outline of Java Program
class FirstProgram {
public static void main(String[] args) {
// body of the program
}
}
The Class Name

Must begin in uppercase letter (A-Z)

If more than one word, use CamelCase (capitalise the first
letter of each word)

DO NOT use underscores! (e.g. My_Program,
student_number)

Name of Java file must be the same as name of class –
otherwise, compilation error is generated
The Main Method

Signature:
public static void main(String[] args)


Argument must be (String[] args), can written as (String args[])


We will declare other methods (functions) in a class, but the main()
function is the one the JVM looks for when executing your code
The Body of main() Method

Body is surrounded by a set of curly braces:
{ : opening brace (the beginning of the body)
// code within braces
} : closing brace (the end of the body)


Each statement in the body MUST end in a semi-colon
(;)
The Variable
The Variable
The Variable
Declaration
int age;

Assignment
age = 21;

Declaration and Assignment


int age = 21;
Eight Primitive Data Types
Data Type Width in Bits Range Declaration Example

byte 8 -128 to 127 byte numLectures = 42;


Short Integer 16 -32,768 to 32,767 short comp102Size = 256;
Integer 32 -2,147,483,648 to 2,147,483, 647 int westvillePop = 50_000;

Long 64 -9,223,372,036,854,775,808 to long saPopulation = 1234567L;


9,223,372,036,854,775,807
32 approximately ±3.40282347E+38F float randToDollar = 15,8; (6-7 significant
decimal digits)
Java implements IEEE 754 standard
Double 64 approximately double circum = 15,2255;
±1.79769313486231570E+308
(15 significant decimal digits)
Character 16 0 to 65,536 (unsigned) char grade = ‘A’;

Boolean (not defined, 1) true or false boolean isHappy = true;


Variable Naming Conventions
(1) Be descriptive:
- BAD: x
- GOOD: interestRate
(2) Keep variables to a reasonable length
- BAD: howMuchPeopleGetPaidEachMonth
- GOOD: salary
(3) Make them as meaningful as possible
- BAD: people
- GOOD: employees
Ends.

You might also like