Java Variables Input and Arrays By John Laban
Java: Variables, Input and Arrays By: John Laban, Hasan Majid, Noah Ventura
Variables - A variable is data given an identifier. - Data that is stored in ram. - A variable can have different data types. - The variable can be referenced anywhere in the code.
Data Types Primitive Data Types: Non-Primitive Data Types: Integer (Int) - Can use mathematical operations. - Contains one whole number. Double - Can use mathematical operations. - Contains one number which can contain decimals. Boolean - Can be in a state of true or false. Char - Contains one letter. String - Largely used for words, sentences and other text based data. - Any numbers stored are non-mathematical. - Can be converted to a int, double, char array etc. - Can use manipulation commands. - Needs to have an imported String Class:
Input - Programs use input to allow interaction between the user and the program. - One form of input is the scanner. - To import the scanner class use: - You then need to create a Scanner object: - After, use the method called “next” to get input from the user, varies per data type:
Arrays - An array is a structure that can store multiple items of the same data type. - An array has a fixed number of data items (called elements). - Each element in an array has an assigned index value starting at 0. - The size of the array is known when the array is initialized: - Data stored in an element can be changed:
Dynamic Arrays - Dynamic Arrays allow you to have an array in your program which can change size during runtime. - Dynamic arrays are initialized with an Array. List: - Dynamic arrays use methods to edit the data:
Displaying All Data in an Array -To print every element in a regular array use this code: -To print every element in a dynamic array use this code:
- Slides: 7