Skip to content

Commit 524e656

Browse files
Variable and DataType
Duration 3 day's 5/9/23 - 8/9/23
1 parent 6c0fa6a commit 524e656

File tree

7 files changed

+329
-0
lines changed

7 files changed

+329
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.Scanner;
2+
3+
public class Assignment {
4+
public static void main(String[] args) {
5+
Scanner sc = new Scanner(System.in);
6+
7+
// Question 1
8+
float A=10f, B=20f, C=10f;
9+
float Average = (A+B+C) / 3;
10+
11+
// Question 2
12+
int side = 5;
13+
int Area = side*side;
14+
15+
// Question 3
16+
float pencil = sc.nextFloat(), pen = sc.nextFloat(), eraser = sc.nextFloat();
17+
float totalCost = pencil + pen + eraser;
18+
float gst = (totalCost /100) *118;
19+
20+
// Question 4
21+
int $ = 12;
22+
System.out.println($);
23+
}
24+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public class BoilerPlate {
2+
public static void main(String[] args) {
3+
4+
}
5+
}
6+
7+
// Boiler Plate is that Code which is written in every java file that's known as Boiler Plate Code
8+
// As you can see this file Code
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
public class DataType {
2+
public static int xpown(int x, int Bit) {
3+
int result=0;
4+
if (Bit==1) {
5+
return 1;
6+
}
7+
return x * xpown(x,Bit-1);
8+
}
9+
10+
public static void main(String[] args) {
11+
12+
// For Checking dataType Range use below statement
13+
// System.out.println(xpown(2,8));
14+
15+
// Byte DataType
16+
byte a = 115; // Range -128 to 127 Size 1 byte
17+
byte b = 12;
18+
byte c = (byte) (a + b);
19+
System.out.println("Byte DataType : "+c);
20+
21+
// Short DataType
22+
short d = 32767; // Range -32768 to 32767 Size 2 byte
23+
short e = 10;
24+
short f = (short)(d + e);
25+
System.out.println("Short DataType : "+e);
26+
27+
// Literal and default math value is taken as a int type datatype
28+
// Int DataType
29+
int num1 = 45; // Range -3B to 3B Size 4 byte
30+
int num2 = 12;
31+
int sum = num2 + num1;
32+
System.out.println("Int DataType : "+sum);
33+
34+
// Double DataType
35+
double o = 342.1123456712343D; // After dot 13 Value allowed to use
36+
System.out.println("Double DataType : "+o); // Size 8 byte
37+
38+
// Float DataType
39+
float g = 342.1123456712343F; // After dot 5 Value allowed to use
40+
System.out.println("Float DataType : "+g); // Size 4 byte
41+
42+
// Long datatype
43+
long l1 = 1231223423; // Longest Integer value it has
44+
System.out.println("long DataType : "+l1);
45+
46+
// Char DataType
47+
char chr = 'a';
48+
char chr1 = 'B';
49+
char result = (char) (chr + chr1);
50+
System.out.println("Char DataType : "+result);
51+
}
52+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* I suggest you to use the .valueOf();
3+
* Converting anything to String use DataTypeToBeConverted.toString(variable);
4+
* This function will work on maximum changing to dataType
5+
*/
6+
7+
import java.util.Scanner;
8+
9+
public class DataType_Conversion {
10+
public static void main(String[] args) {
11+
Scanner sc = new Scanner(System.in);
12+
13+
/* ***************** 1 ****************** */
14+
15+
// 1. String to Int and long
16+
// Here, We need Exception Handling. String have all character, number, special character(!@#$%^&*()) that's why we need EH.
17+
try {
18+
String str = sc.next();
19+
20+
int num = Integer.parseInt(str);
21+
int value = Integer.valueOf(str);
22+
23+
long num1 = Long.parseLong(str);
24+
long value1 = Long.valueOf(str);
25+
26+
} catch (Exception e) {
27+
System.out.println(e);
28+
}
29+
30+
// 2. String to Byte and Short
31+
// Here, also need Exception Handling.
32+
try {
33+
String str = sc.next();
34+
35+
Byte num = Byte.parseByte(str);
36+
Byte value = Byte.valueOf(str);
37+
38+
Short num1 = Short.parseShort(str);
39+
Short value1 = Short.valueOf(str);
40+
41+
} catch (Exception e) {
42+
System.out.println(e);
43+
}
44+
45+
// 3. String to Float and double
46+
try {
47+
String str = sc.next();
48+
49+
float num = Float.parseFloat(str);
50+
double value = Double.parseDouble(str);
51+
52+
System.out.println(num+" "+value);
53+
} catch (Exception e) {
54+
System.out.println(e);
55+
}
56+
57+
// 4. String to Char
58+
// We have to function for this 1. .charAt(index), 2. .toCharArray()
59+
// Here, no need exception handling because we use directly index, and char can take all type of input
60+
String str = "Hello";
61+
char ch = str.charAt(0);
62+
System.out.println(ch);
63+
char[] chr = str.toCharArray();
64+
for (int i=0; i<chr.length; i++) {
65+
System.out.println(chr[i]);
66+
}
67+
68+
// 5. String to Boolean
69+
Boolean bool = Boolean.parseBoolean(str);
70+
71+
/* ****************** 2 ******************** */
72+
73+
// 1. Int to String
74+
// No need of Exception Handling
75+
int number = 123;
76+
// i.) String.valueOf()
77+
String str0 = String.valueOf(number);
78+
// ii.) Integer.toString()
79+
String str1 = Integer.toString(number);
80+
// iii.) String.format()
81+
String str3 = String.format("%s", str);
82+
83+
// 2. Int to long
84+
// No need more code for this Simply initialize the value
85+
long l = number;
86+
// long.valueOf();
87+
long l2 = Long.valueOf(number);
88+
// (long)
89+
long l3 = (long) number;
90+
91+
// 3. Int to byte
92+
// Simply use the TypeCasting
93+
byte b = Byte.valueOf((byte) number);
94+
byte b1 = (byte) number;
95+
96+
// 4. Int to Double
97+
double d = Double.valueOf(number);
98+
double d1 = number;
99+
100+
// 5. Int to char
101+
// Simply use Type Casting
102+
char ch1 = (char) number;
103+
104+
/* ********************* 3 *********************** */
105+
106+
// Long to String
107+
String str4 = String.valueOf(l2);
108+
String str5 = Long.toString(l2);
109+
110+
// Long to Char
111+
// Simply use the TypeCasting
112+
char ch2 = (char) l2;
113+
114+
// Long to Byte
115+
byte b2 = (byte) l2;
116+
117+
// Long to double
118+
double d5 = (double) l2;
119+
Double d6 = Double.valueOf(l2);
120+
121+
/* *********************** 4 ************************** */
122+
123+
// Char to String
124+
char character = '1';
125+
String str6 = String.valueOf(character);
126+
String str8 = Character.toString(character);
127+
String str7 = (String) str6;
128+
129+
// Char to Int
130+
// ASCII Value get
131+
int a = character;
132+
// Convert into real Int
133+
int val = Integer.parseInt(String.valueOf(character));
134+
int value = Character.getNumericValue(character);
135+
System.out.println(val+" "+value);
136+
}
137+
}

Variable and DataType/Input.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import java.util.Scanner;
2+
public class Input {
3+
public static void main(String[] args) {
4+
Scanner sc = new Scanner(System.in);
5+
6+
/*
7+
* If you enter the out of range value , then it will give the error "Out of Range"
8+
*/
9+
10+
// Different Type of DataType INPUT from the user
11+
12+
// Byte
13+
System.out.print("Byte : ");
14+
byte b = sc.nextByte();
15+
System.out.println("Byte : "+b);
16+
17+
// Short
18+
System.out.print("Short : ");
19+
short s = sc.nextShort();
20+
System.out.println("Short : "+s);
21+
22+
// Int
23+
System.out.print("Int : ");
24+
int i = sc.nextInt();
25+
System.out.println("Int : "+i);
26+
27+
// Long
28+
System.out.print("Long : ");
29+
long l = sc.nextLong();
30+
System.out.println("Long : "+l);
31+
32+
// Single Word
33+
System.out.print("String Single Word : ");
34+
String sW = sc.next();
35+
System.out.println("String Single Word : "+sW);
36+
37+
/*
38+
* What occurs when we utilize the .next() and .nextLine() functions in conjunction with a message on the output screen?
39+
* It will collect both inputs simultaneously and subsequently display the message, as exemplified in this program.
40+
*/
41+
42+
// Multiple Word
43+
System.out.print("Complete Sentence : ");
44+
String mW = sc.nextLine();
45+
System.out.println("String Multiple Words : "+mW);
46+
47+
// Float
48+
System.out.print("Float : ");
49+
float f = sc.nextFloat();
50+
System.out.println("Float : "+f);
51+
52+
// Double
53+
System.out.print("Double : ");
54+
double d = sc.nextDouble();
55+
System.out.println("Double : "+d);
56+
57+
// Boolean
58+
System.out.print("Boolean : ");
59+
boolean bool = sc.nextBoolean();
60+
System.out.println("Boolean : "+bool);
61+
62+
// Char
63+
System.out.print("Char : ");
64+
char chr = sc.next().charAt(0); // here this is the indexing of the element to take
65+
System.out.print("Char : "+chr);
66+
}
67+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class TypeCasting {
2+
public static void main(String[] args) {
3+
// Type Conversion is also known as Narrowing and Explicit
4+
// Changing any dataType forcefully
5+
6+
// Float to int
7+
float f = 99.67f;
8+
int i = (int) f;
9+
System.out.println(i);
10+
11+
// float to long
12+
long l = (long) f;
13+
System.out.println(l);
14+
}
15+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class TypeConversion {
2+
public static void main(String[] args) {
3+
4+
// byte->short->int->float->long->double
5+
// byte -> short,int,float,long,double
6+
// float -> long,double
7+
8+
// Byte to int
9+
byte b = 123;
10+
int i = b+3;
11+
System.out.println(i);
12+
13+
// Long to short
14+
// lossy conversion because long to short means
15+
// Large_space data is transferring to Short_space and that's not possible that's why it is lossy conversion
16+
long l = 123456;
17+
18+
// It's give error - Lossy Conversion
19+
// short s = l;
20+
// System.out.println(s);
21+
22+
float f = 12.89f;
23+
double d = f;
24+
System.out.println(d);
25+
}
26+
}

0 commit comments

Comments
 (0)