Summary

This document provides a detailed overview of Java programming basics. It covers fundamental concepts such as class headings, main methods, comments, and data types. The material also highlights best practices for program readability. The document also includes examples and explanations for common operations.

Full Transcript

Topic 5: Java Basics Reference: Introduction to programming Java: With a problem-solving approach, 2nd Edition – Chapter 3: pages 62-101 Class Heading Class: is simply a container for the program code. It is the file name. Class heading: public class Hello public: class is acces...

Topic 5: Java Basics Reference: Introduction to programming Java: With a problem-solving approach, 2nd Edition – Chapter 3: pages 62-101 Class Heading Class: is simply a container for the program code. It is the file name. Class heading: public class Hello public: class is accessible by other classes or programs class: identifies the beginning of class code Hello: class name. (class name describes the task of class) main Method Heading main Method: is where the computer starts execution of code. main Method Heading: public static void main(String[] args) public: everyone can access the method static: method is accessible from any class void: method doesn’t return a value main: reserved name (keyword) String[] args: arguments for main method Class and main Method Headings Braces { } : to denote a block of statements. Opening brace { determines the beginning of a class or a method Closing brace } determines the end of a class or a method Comments Comments: Text within the code that is intended for human readers and is ignored by the compiler. Types of comments: One-line: // Block-comment: Comments Example: public class Hello { public static void main(String[] args) { System.out.println("Hello World!"); //print on screen } } Readability Increase the program readability by: Comments: add explanations to the code. Blank lines: makes the program easy to read and understand. Hard to understand Easy to understand public class Hello{public static void public class Hello main(String[] args) { {System.out.println("Hello World! ");}} public static void main(String[] args) { System.out.println("Hello World! "); } } Getting Inputs Scanner stdIn = new Scanner(System.in): takes inputs from keyboard. stdIn is the name of the scanner Note: the statement “import java.util.Scanner;” should be used to use the scanner. Examples: int x = stdIn.nextInt(); float y = stdIn.nextFloat(); double w = stdIn.nextDouble(); char r = stdIn.next().charAt(0); String s = stdIn.nextLine(); Giving Outputs System.out.println( ) statement gives output on the screen. Examples: Printing a single message: ;System.out.println("Hello World! ") Printing two messages one after another: ;System.out.println("I am " + "a programmer") Printing the value of the variable “result”: ;System.out.println(result) Keywords Keywords: are words that have already been defined for Java compiler. They have special meaning for the compiler and cannot be used as a variable, class or a method name. Examples: public,static,class,import (keywords for defining parts of code) int, float, double, char (keywords for data types) if, else, switch (keywords for conditions) for, while (keywords for loops) Identifiers Identifier: a name of any component in the program (a class, method, or variable) Examples: Hello , main, x, a2. Letters (A-Z) , numbers (0-9), _ , $ can be used (use any combination) Don’t start with a number Cannot use keywords as names Recommendation: The identifier should be descriptive. Class name should start with an uppercase e.g. Hello Method name should start with a lowercase e.g. printResult Variables Variable: is a reserved memory location to store a value. Variable can only hold one type of value. Variable Datatypes: here are some datatypes: int stores whole numbers up to 231-1. float stores decimal numbers up to 7 digits. double stores decimal numbers up to 16 digits. char stores a symbol (letter, keyboard symbols, digits …). String stores text (words, sentences, paragraphs …). Variables Declaration Declaration of variables: allocates a memory location for a variable with a specific name and datatype. Examples: int a1; float dist; double result; char op; String msg; Variables Assignment Assignment: stores a value to a memory location of a variable. Examples: a1 = 25; dist = 103.25f ; result = 9.38524; Op = ‘*’; msg = “Welcome”; Declaration and Initialization Declaration and Initialization: allocates a memory location for a variable with a specific name and datatype and stores a value in that location in one command. Examples: int a1 = 25; float dist = 103.25f; double result = 9.38524; char op =‘*’; String msg = “Welcome"; Constants Constant: a variable with a fixed value that cannot be changed in the program. Examples: final int SPEEDOFSOUND = 343; final float PI = 3.1415926f; Recommendation: Variable’s name should start with a lowercase e.g. firstName Constant’s name is all capitalized e.g. NUMBER Variables and Type Casting Type casting: to convert the value from one data type to another. Conversion from smaller to larger datatypes is done automatically, but some precision could be lost. ------range increase----> int < float < double Use the cast operator (int), (float), (double) to convert between different datatypes. Variables and Type Casting Examples for automatic type casting: From integer to float: float x = 5; From integer to double: double r = 92; From float to double: double r = 3.78f; Examples for manual type casting: From float to integer: int x = (int) 5.25f; From integer to float: float r = (float) 41/3; From integer to double: double r = (double) 41/3; Arithmetic Operations Addition: a + b Subtraction: x – y Multiplication: d * r Division: s / h Modulus: k % q Operator precedence order: Parenthesis: () Exponent: XY Multiplication and Division: *,/ Addition and Subtraction: +,- Relational Operations Greater than: a > 6 Less than: x < 15 Greater than or equal: c >= d Less than or equal: p

Use Quizgecko on...
Browser
Browser