Starting Out with Java: Java Fundamentals PDF

Summary

This presentation by Dr. Muhammad Faheem, from GIFT UNIVERSITY, covers Java fundamentals, including boolean, char, and arithmetic operators. The slides were given on November 24, 2020, and focus on aspects of programming and Java.

Full Transcript

Starting Out with Java From Control Structures through Objects Session 4 (Chapter 2): Java Fundamentals Dr. Muhammad Faheem November 24, 2020 1 / 10 boolean Data Types Following expressions have either Yes...

Starting Out with Java From Control Structures through Objects Session 4 (Chapter 2): Java Fundamentals Dr. Muhammad Faheem November 24, 2020 1 / 10 boolean Data Types Following expressions have either Yes or No answers: 1) Is 7 greater than 2 2) Is 11 equal to 11 3) Is it end of file For such expression, Java has boolean data type. The boolean data type can have two possible values: True or false. Example: Numeric Data Types boolean flag = true; boolean isPresent = false; We will explore the boolean data type in chapter 3. 2 / 10 char Data Types Java char data type provides access to single characters. char data type can respresent any key on your keyboard such as letter, digit, and special symbol. char literals are enclosed in single quote marks. Example: ‘A’, ‘a’, ‘!’, ‘7’ Blank space is a single character and can be assigned to a char type of Variable. Example: char ch1 = ‘7’; char ch2 = ‘ ’; char ch3 = "g"; // Error becuase of "" 3 / 10 Unicode Internally, characters are stored as numbers. Character data in Java is stored as Unicode characters. Each Unicode number requires two bytes of memory, so char variables occupy two bytes. The number 65 is the code for the character A, 66 for B and so on. See Appendix B from book Example: char ch1 = 65; // Storing the code of char A char ch2 = 66; System.out.println(ch1); // Output will be A System.out.println(ch2); // Output will be B 4 / 10 Example: boolean and char Data Types See the Code Example 1 for Session 4! 5 / 10 Arithmetic Operators Arithmetic Expressions: Contain values and arithmetic operators. Operands: Number of values on which the operators will work. Operators: Unary (one operand) or Binary (two operands) or Ternary. Example of Unary Operator: int num1 = 5; int num2 = -num1; //- act as unary operator 6 / 10 Arithmetic Operators Operator Meaning Type Example + Addition Binary total = cost + tax; − Subtraction Binary netIncome = totalRevenue − totalExpenses; ∗ Multiplication Binary paymentDue = itemPrice ∗ noOfItems; / Division Binary itemPrice = payementDue / noOfItems; % Modulus Binary remainder = value % 5; 7 / 10 Arithmetic Operators Why Binary? It involes two operands. Integer Division: Compiler Error: When dividing a number by zero. Integer division is tricky. Example: 1/2 Output: 0 (expected 0.5) Why: Integer division will truncate any decimal remainder! Example (Usage of Integer Division): Buy 2 get 1 Free Pizza; Write a program to calculate the number of free pizza(s) on each purchase. 8 / 10 Example: Arithemetic Operators See the Code Example 2 for Session 4! 9 / 10 Credit Tony Gaddis, Starting out with Java: From Control Structures through Objects, 6th Edition, Pearson 2016. 10 / 10

Use Quizgecko on...
Browser
Browser