Java Programming: Comments, Data Types PDF
Document Details
Uploaded by OpulentDevotion6404
Itahari International College
2024
Jeevan Poudel
Tags
Summary
This document is a lecture on Java programming, specifically covering comments, data types, and literals. It details primitive data types, like integers and floating-point numbers, and non-primitive types. Examples are provided for each data type.
Full Transcript
COMMENTS, DATA TYPES WEEK 3: LECTURE Jeevan Poudel November 25, 2024 Itahari International College CONTENTS I 1. Comments Single Line Comment Multi-Line comment Documentation Comment 2. Data Types Primitive and Non-primitive types Integer Types FLoating point types Character...
COMMENTS, DATA TYPES WEEK 3: LECTURE Jeevan Poudel November 25, 2024 Itahari International College CONTENTS I 1. Comments Single Line Comment Multi-Line comment Documentation Comment 2. Data Types Primitive and Non-primitive types Integer Types FLoating point types Character type Boolean Type Byte CS4001NT Comments, Data Types November 25, 2024 1 / 26 CONTENTS II Short Int Long Float Double Boolean Char Example of all data types Default Values 3. Literals CS4001NT Comments, Data Types November 25, 2024 2 / 26 CONTENTS III Types of literals CS4001NT Comments, Data Types November 25, 2024 3 / 26 COMMENTS Comments COMMENTS Comment is used to explain or describe part of a program and it does not affect execution of program Three ways of marking comments in Java: 1. Single line comment 2. Multi-line comment 3. Documentation comment CS4001NT Comments, Data Types November 25, 2024 4 / 26 COMMENTS SINGLE LINE COMMENT Comments Single Line Comment SINGLE LINE COMMENT Denoted by double forward slash // Runs from // to the end of the line Example 1 public static void main ( String args []) { 2 // This is a secret formula to create a secret program 3 int secretFormula = a*a + 2*a*b + b*b; 4 } CS4001NT Comments, Data Types November 25, 2024 5 / 26 COMMENTS MULTI-LINE COMMENT Comments Multi-Line comment MULTI-LINE COMMENT Starts with Used for commenting out multiple line or certain block Example 1 public static void main ( String args []) { 2 6 7 11 } CS4001NT Comments, Data Types November 25, 2024 6 / 26 COMMENTS DOCUMENTATION COMMENT Comments Documentation Comment DOCUMENTATION COMMENT Used to generate documentation automatically from the source code Uses to end. Example 1 CS4001NT Comments, Data Types November 25, 2024 7 / 26 DATA TYPES Data Types DATA TYPES How to store your name/email? How to store someone’s age? How to store a single character How to store your CGPA score? Java is a strongly typed language. This means that every variable must have a declared type. The data types in Java categorized as: 1. Primitive types 2. Non-primitive types CS4001NT Comments, Data Types November 25, 2024 8 / 26 DATA TYPES PRIMITIVE AND NON-PRIMITIVE TYPES Data Types Primitive and Non-primitive types PRIMITIVE AND NON-PRIMITIVE TYPES A primitive type is predefined by the language and is named by a reserved keyword Primitive values do not share state with other primitive values The eight primitive data types supported by the Java 1. byte 3. int 5. float 7. boolean 2. short 4. long 6. double 8. char CS4001NT Comments, Data Types November 25, 2024 9 / 26 Data Types Primitive and Non-primitive types PRIMITIVE AND NON-PRIMITIVE TYPES 1. Primitive types A. Numeric type a. Integer types −→ byte, short, int, long b. Floating point type −→ float, double B. Non-numeric types a. Boolean type −→ boolean b. Character type char 2. Non-primitive types a. Class b. Interface c. Array d. String CS4001NT Comments, Data Types November 25, 2024 10 / 26 DATA TYPES INTEGER TYPES Data Types Integer Types INTEGER TYPES Integer types are for numbers without fractional parts. Negative values are allowed. Java provides the four integer types: 1. byte 2. short 3. int 4. long Example 1 byte age = 16; 2 short noStudents = 500; 3 int populationOfTown = 225000; 4 long veryLargeNumber = 223467859 L; CS4001NT Comments, Data Types November 25, 2024 11 / 26 DATA TYPES FLOATING POINT TYPES Data Types FLoating point types FLOATING POINT TYPES Floating-point types denote numbers with fractional parts The name double refers to the fact that these numbers have twice the precision of the float type Numbers of type float have a suffix F or f (for example, 3.14F) Example 1 float waterLevel = 5000.689 f; 2 double accuracyOfDna = 57893.56788993 d; // can ommit d suffix CS4001NT Comments, Data Types November 25, 2024 12 / 26 DATA TYPES CHARACTER TYPE Data Types Character type THE CHARACTER TYPE Literal values of type char are enclosed in single quotes For example, 'A' is a character constant with value 65. It is different from "A", a string containing a single character. Values of type char can be expressed as hexadecimal values that run from \u0000 to \uFFFF Example 1 char confirmation = 'y'; CS4001NT Comments, Data Types November 25, 2024 13 / 26 DATA TYPES BOOLEAN TYPE Data Types Boolean Type BOOLEAN TYPE The boolean type has two values: 1. false and 2. true It is used for evaluating logical conditions Example 1 boolean isStudent = true; 2 boolean isAdult = false ; CS4001NT Comments, Data Types November 25, 2024 14 / 26 DATA TYPES BYTE Data Types Byte BYTE An 8 bit signed integer Minimum value −128 and a Maximum value of 127 (inclusive) Useful for saving memory in large arrays Default value is 0 (Zero) CS4001NT Comments, Data Types November 25, 2024 15 / 26 DATA TYPES SHORT Data Types Short SHORT A 16-bit signed integer MIN value −32, 768 (−215 ) and MAX value 32, 767(inclusive) 215 − 1 Default value is 0 CS4001NT Comments, Data Types November 25, 2024 16 / 26 DATA TYPES INT Data Types Int INT Is a 32-bit signed integer Has a MIN value of −231 and a MAX value of 231 − 1 Default value is 0 Default data type for integral values unless there is a concern about memory CS4001NT Comments, Data Types November 25, 2024 17 / 26 DATA TYPES LONG Data Types Long LONG Is a 64-bit signed integer Has a MIN value of −263 and a MAX value of 263 − 1 Default value is 0L Used when a wider range than int is needed CS4001NT Comments, Data Types November 25, 2024 18 / 26 DATA TYPES FLOAT Data Types Float FLOAT Float data type is a single-precision 32-bit floating point Mainly used to save memory in large arrays of floating point numbers Default value is 0.0f Float data type is never used for precise values such as currency CS4001NT Comments, Data Types November 25, 2024 19 / 26 DATA TYPES DOUBLE Data Types Double DOUBLE Double data type is a double-precision 64-bit floating point Generally used as the default data type for decimal values Default value is 0.0d Double data type is never used for precise values such as currency (In Java BigDecimal can be used to store currency values) CS4001NT Comments, Data Types November 25, 2024 20 / 26 DATA TYPES BOOLEAN Data Types Boolean BOOLEAN Used to represent one bit of information Only two possible values: true and false Used for simple flags that track true/false conditions CS4001NT Comments, Data Types November 25, 2024 21 / 26 DATA TYPES CHAR Data Types Char CHAR The char type was originally intended to describe individual 16-bit unicode character Minimum value is \u0000 (or 0) Maximum value is \uFFFF (or 65,535 inclusive) CS4001NT Comments, Data Types November 25, 2024 22 / 26 DATA TYPES EXAMPLE OF ALL DATA TYPES Data Types Example of all data types EXAMPLE OF ALL DATA TYPES Example 1 // class not shown here 2 public static void main( String args []) { 3 byte age = 15; 4 short noOfStudents = 300; 5 int villagePopulation = 25000; 6 float answer = 2.25f; 7 double score = 3.5d; 8 char confirmation = 'y'; 9 boolean isOdd = true; 10 } CS4001NT Comments, Data Types November 25, 2024 23 / 26 DATA TYPES DEFAULT VALUES Data Types Default Values DEFAULT VALUES It’s not always necessary to assign a value when a field is declared Fields that are declared but not initialized will be set to a reasonable default by the compiler The default will be zero or null, depending on the data type Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable Accessing an uninitialized local variable will result in a compile-time error Example 1 int age; 2 String message ; 3 boolean isStudet ; CS4001NT Comments, Data Types November 25, 2024 24 / 26 LITERALS Literals Regardless of its complexity, a program always performs operations on numbers, strings, and other values These values are called literals Literals can represent various types of values such as numeric, character, boolean, or string values. We can assign literals to any of the 8 primitive data types in Java CS4001NT Comments, Data Types November 25, 2024 25 / 26 LITERALS TYPES OF LITERALS Literals Types of literals TYPES OF LITERALS 1. Integral literals 3. Char literal 5. String literal 2. Floating point literal 4. Boolean literal Example 1 // integral literals 2 int speed = 200; 3 4 // floating point literals 5 float score = 3.45f; 6 double measurement = 3.4567 d; 7 8 // string Literals 9 String collegeName = " Itahari International College "; 10 11 // character Literals 12 char confirmation ='N'; 13 CS4001NT Comments, Data Types November 25, 2024 26 / 26