Lecture notes:
Camel case - Java follows the camel-case syntax for naming the classes, interfaces,
methods, and variables. If the name is combined with two words, the second word will
start with uppercase letter always, such as maxMarks( ), lastName, ClassTest, removing
all the whitespaces
Data types:
Integer - whole number
Character - Stores a single character/letter. enclosed in a single quotation mark
String - collection of characters. enclosed in a double quotation mark
Example:
"a" - String
'a' - Character
'hello' - Error
"hello" - String
Boolean - Stores true or false values
Double - Stores fractional numbers. Sufficient for storing 15 decimal digits
Float - Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
Array - used to store multiple with the same data type values in a single variable.
- size of the array is fixed, and we cannot directly add a new element in Array.
Exception: We can add elements in an array by creating special
methods/functions design for that specific array.
Examples:
String hello = "Hello";
Integer firstNum = 2;
Integer secondNum = ;
double fourthNum = 14.2;
float fifthNum = 3.1416f;
boolean isAnswerCorrect = false;
String[] cars = { "honda", "toyota", "mazda" };
Type Casting
• assign a value of one primitive data type to another type
• Applicable only to numerical datatype/ primitive data type
2 Type of type casting:
• Widening - done automatically when passing a smaller size type to a larger size
type. Ex: int -> double
int i = 200;
double l = i;
// System.out.println(l);
• Narrowing - casting a large type to a smaller type size. Narrowing casting must
be done manually by placing the type in parentheses in front of the value
double pi = 3.1416;
int a = (int) pi;
// System.out.println(a);
Java Type Conversion
• string to numerical data type; vice-versa
Examples:
String to integer
String s="200";
int i=Integer.parseInt(s);
Integer to string
it s= 200;
int i=String.valueOf (s);
Array methods:
You can access an array element by referring to the index number
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars[0]);
// Outputs Volvo
To change the value of a specific element, refer to the index number
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
System.out.println(cars[0]);
// Now outputs Opel instead of Volvo
To find out how many elements an array has, use the length
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars.length);
// Outputs 4