🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Variables and Data Types.pptx

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Transcript

VARIABLES and DATA TYPES VARIABLES Learning Objectives: Define and use variables of different data types. (Demonstration) Create and manipulate variables. VARIABLE is a named memory location that holds a value or data. is typically assigned with a data type. ...

VARIABLES and DATA TYPES VARIABLES Learning Objectives: Define and use variables of different data types. (Demonstration) Create and manipulate variables. VARIABLE is a named memory location that holds a value or data. is typically assigned with a data type. COMPONENTS OF A When VARIABLE declaring a variable, there are two components: Data type Variable name When initializing a variable, there are three components: Data type Variable name WHEN DECLARING A VARIABLE... DATA TYPE int year; VARIABL E NAME WHEN INITIALIZING A VARIABLE... DATA TYPE int year = 2024; VARIABL VALUE E NAME VARIABLE comes in three types based on their scope and where they are declared: Local Variable Instance Variable LOCAL VARIABLE Definition: a variable declared inside a method, constructor, or block Scope: Accessible only within the method, constructor, or block in which they are declared Initialization: Must be initialized before use, as they do not have a Example of a LOCAL VARIABLE public void displayYear() { int year = 2024 ; // Local variable System.out.println(number); } INSTANCE VARIABLE (NON-STATIC FIELD) Definition: A variable declared within a class but outside any method, constructor, or block. Scope: Each object of the class has its own copy of instance variables. Initialization: Initialized with default values if not explicitly initialized. For Example of an INSTANCE VARIABLE public class BigBike { String brand; // Instance variable String model; // Instance variable int year; // Instance variable } STATIC VARIABLE (CLASS VARIABLE) Definition: A variable declared with the static keyword inside a class, but outside any method, constructor, or block. Scope: Shared among all instances of the class, meaning only one copy of the variable is created and shared among all objects. Initialization: Initialized with default STATIC VARIABLE (CLASS VARIABLE) Definition: A variable declared with the static keyword inside a class, but outside any method, constructor, or block. Scope: Shared among all instances of the class, meaning only one copy of the variable is created and shared among all objects. Initialization: Initialized with default Example of a STATIC VARIABLE public class Bike { static int numberOfBikes; // Static variable } OTHER CONCEPTS OF VARIABLES Variable Type Scope Description Final Variable Once initialized, Constant variable There are other cannot change types of variables Reference Refers to objects Stores memory address Variable of an object that are Global Variable In Java, simulated Accessible throughout distinguished by static the application based on their Parameter Method-specific Passed into methods for Variable use inside characteristics or behavior. FINAL VARIABLE A variable declared with the final keyword cannot be changed once initialized. It can be local, instance, or static. Ex. final int PASSING_SCORE = 75; REFERENCE VARIABLE Stores the address (or reference) of an object in memory rather than the object itself. All non-primitive types (such as objects, arrays, and Strings) in Java are stored as reference variables. Ex. String str = "Hello"; // str is a reference variable pointing to the String GLOBAL VARIABLE Static variables in Java can act similarly to global variables because they are accessible across all instances of a class. Ex. public class AppConfig { public static String appName = "MyApp"; // Acts like a global variable } public class Main { public static void main(String[] args) { System.out.println(AppConfig.appName); // Accessible from anywhere } PARAMETER VARIABLE Variables declared within the method signature and used to pass values into methods. These variables are treated as local variables inside the method Ex. public void greet(String name) { // name is a parameter variable System.out.println(”Good Morning " + name); } DATA TYPES DATA TYPES specify the different sizes and values that can be stored in the variable comes in two different types: Primitive Data Types and Non-primitive Data Types DATA TYPES AT A GLANCE Source: https://www.geeksforgeeks.org/data-types-in-java/ PRIMITIVE DATA TYPES includes byte, short, int, long, float, double, boolea n, and char Source: https://www.w3schools.com/java/java_data_types.asp BOOLEAN represents a logical value that can be either true or false. is typically one byte (eight bits) in practice. Ex. boolean myChoice = true; boolean isEmpty = false; CHAR is a single 16-bit Unicode character with the size of 2 bytes (16 bits) Ex. char middleInitial = ‘S’; char hashtag = ‘#’; BYTE is an 8-bit signed two's complement integer. is useful for saving memory in large arrays. Ex. byte myGrade = 95; byte age = 24; SHORT is a 16-bit signed two’s complement integer. is useful for saving memory in large arrays, in situations where the memory savings actually matters. Ex. short salary = 29165; short price = 14990; INT is a 32-bit signed two’s complement integer.. Ex. int currentBalance = 890000; int budget = 3550000; LONG is a 64-bit signed two’s complement integer.. Ex. long money = 656786469253; long savings = 3550000; FLOAT is a single-precision 32-bit IEEE 754 floating- point. Ex. float quotient = 12.6474f; float num1 = 75.124677f; DOUBLE is a double-precision 64-bit IEEE 754 floating- point. For decimal values, this data type is generally the default choice. Ex. double x = 5667.64741223; double quot = 75881.124677696; NON-PRIMITIVE DATA TYPES include Strings, Arrays, and Objects. STRING is defined as an array of characters, thus, can hold more than one character unlike the char datatype. Ex. String mySchool= “Davao Oriental State University”; String myCourse = “BS in Information Technology”; ARRAY is a group of like-typed variables that are referred to by a common name. Ex. String[] cars = {”Hyundai", ”Toyota", ”Ferrari", ”Ford"}; int[] myGrades = {98, 96, 91, 94, 97}; OBJECTS is a basic unit of Object-Oriented Programming and represents real-life entities. Ex. public class Main { int x = 5; public static void main(String[] args) { Main myObj1 = new Main(); // Object 1 Main myObj2 = new Main(); // Object 2 System.out.println(myObj1.x); System.out.println(myObj2.x); } } THANK YOU! Summary REFERENCES: https://www.linkedin.com/pulse/ everything-you-need-know-java- variables-data-types-dotnettricks/ https://www.w3schools.com/java/ https://www.geeksforgeeks.org/data- types-in-java/ https://www.javatpoint.com/java-data- types

Tags

data types programming variables computer science
Use Quizgecko on...
Browser
Browser