Java - Introduction to Programming
Lecture 2
Variables & Data Types
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;
String neighbour = "Akku";
String friend = neighbour;
}
}
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.
* 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;
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;
public class Main {
public static void main(String[] args) {
// Constants
final float PI = 3.14F;
}
}
Homework Problems
Try to declare meaningful variables of each type. Eg - a variable named age should be a numeric type (int or float) not byte.
Make a program that takes the radius of a circle as input, calculates its radius and area and prints it as output to the user.
Make a program that prints the table of a number that is input by the user.
(HINT - You will have to write 10 lines for this but as we proceed in the course you will be studying about ‘LOOPS’ that will simplify your work A LOT!)
KEEP LEARNING & KEEP PRACTICING :)
Variables in Programming:-
Data Types in Programming:-
Numeric Data Types:-
Boolean Data Type:-
String Data Type:-
Conclusion:-
In conclusion, variables and data types are fundamental concepts in programming. Variables are used to store data, while data types represent the different kinds of data that can be stored in a variable. Understanding variables and data types is crucial in programming, as they form the foundation of all programs. By mastering these concepts, you will be well on your way to becoming a skilled programmer.
Others blog:-
- insertion sort time complexity
- Inheritance | types of inheritance | inheritance in python
- Polymorphism in java | polymorphism genetics
- Bit Manipulation
- Package in Java| what is the case used for a package in java| creating packages in java
- https://javacodingplacement.blogspot.com/2023/03/insertion-sort-insertion-sort-algorithm.html
Comments
Post a Comment