Nội dung text Java Notes
JAVA 1. Install Java a. Install JDK (https://www.oracle.com/in/java/technologies/javase-downloads.html) b. Install IntelliJ (https://www.jetbrains.com/idea/download/#section=mac) 2. Sample Code Functions A function is a block of code which takes some input, performs some operations and returns some output. The functions stored inside classes are called methods. The function we have used is called main. Class A class is a group of objects which have common properties. A class can have some properties and functions (called methods). The class we have used is Main. 3. Our 1st Program package com.apnacollege; public class Main { public static void main(String[] args) { // Our 1st Program System.out.println("Hello World"); } } 4. Variables A variable is a container (storage area) used to hold data. Each variable should be given a unique name (identifier). package com.apnacollege; public class Main { public static void main(String[] args) { // Variables String name = "Aman"; int age = 30; Apni Kaksha
String neighbour = "Akku"; String friend = neighbour; } } 5. Data Types Data types are declarations for variables. This determines the type and size of data associated with variables which is essential to know since different data types occupy different sizes of memory. There are 2 types of Data Types : - Primitive Data types : to store simple values - Non-Primitive Data types : to store complex values Primitive Data Types These are the data types of fixed size. Data Type Meaning Size (in Bytes) Range byte 2’s complement integer 1 -128 to 127 short 2’s complement integer 2 -32K to 32K int Integer numbers 4 -2B to 2B long 2’s complement integer (larger values) 8 -9,223,372,036,85 4,775,808 to 9,223,372,036,85 4,775,807 float Floating-point 4 Upto 7 decimal digits double Double Floating-point 8 Upto 16 decimal digits Apni Kaksha
char Character 2 a, b, c .. A, B, C .. @, #, $ .. bool Boolean 1 True, false Non-Primitive Data Types These are of variable size & are usually declared with a ‘new’ keyword. Eg : String, Arrays String name = new String("Aman"); int[] marks = new int[3]; marks[0] = 97; marks[1] = 98; marks[2] = 95; 6. String Class Strings are immutable non-primitive data types in Java. Once a string is created it’s value cannot be changed i.e. if we wish to alter its value then a new string with a new value has to be created. This class in java has various important methods that can be used for Java objects. These include: a. Concatenation String name1 = new String("Aman"); String description = new String("is a good boy."); String sentence = name1 + description; System.out.println(sentence); b. CharAt String name = new String("Aman"); System.out.println(name.charAt(0)); c. Length String name = new String("Aman"); System.out.println(name.length()); d. Replace String name = new String("Aman"); System.out.println(name.replace('a', 'b')); Apni Kaksha
e. Substring String name = new String("AmanAndAkku"); System.out.println(name.substring(0, 4)); 7. Arrays Arrays in Java are like a list of elements of the same type i.e. a list of integers, a list of booleans etc. a. Creating an Array (method 1) - with new keyword int[] marks = new int[3]; marks[0] = 97; marks[1] = 98; marks[2] = 95; b. Creating an Array (method 2) int[] marks = {98, 97, 95}; 8. Casting Casting in java is the assigning values of one type to another. The types being considered here are compatible i.e. we can only assign values of a number type to another type storing numbers (vice-versa is not allowed i.e. floating values cannot be assigned to boolean data types). Casting in Java is of 2 types: a. Implicit casting This casting is done by java implicitly i.e. on its own. It is assigning smaller values to larger data types. float price = 100.00F; int gst = 18; float finalPrice = price + gst; b. Explicit casting This casting is done by the programmer. It is assigning larger values to smaller data types. int price = 100; float gst = 18.00F; int finalPrice = price + (int)gst; 9. Constants A constant is a variable in Java which has a fixed value i.e. it cannot be assigned a different value once assigned. package com.apnacollege; Apni Kaksha