File tree Expand file tree Collapse file tree 8 files changed +217
-1
lines changed
JavaBasicPrograms/src/main/java Expand file tree Collapse file tree 8 files changed +217
-1
lines changed Original file line number Diff line number Diff line change 1+ package learnConditionalStatemt ;
2+
3+ public class DrinkTypes {
4+
5+ String myDrink = "Milk" ;
6+
7+ public void favDrink () {
8+ if (myDrink .equals ("Coffee" )) {
9+ System .out .println ("Coffee is my drink" );
10+ }
11+ else if (myDrink .equals ("Boost" )) {
12+ System .out .println ("Boost is my drink" );
13+ }
14+
15+ else if (myDrink .equals ("Milk" )) {
16+ System .out .println ("Milk is my drink" );
17+ }
18+
19+ else {
20+ System .out .println ("Tea is my Drink" );
21+ }
22+
23+ }
24+
25+
26+ public static void main (String [] args ) {
27+ DrinkTypes drink = new DrinkTypes ();
28+ drink .favDrink ();
29+
30+ }
31+
32+ }
Original file line number Diff line number Diff line change 1+ package learnConditionalStatemt ;
2+
3+ public class IfElseConditionChk {
4+
5+ boolean emptyCup ; // default value of boolean data type is 'false'
6+
7+ public static void main (String [] args ) {
8+
9+ IfElseConditionChk cup = new IfElseConditionChk ();
10+
11+ if (cup .emptyCup == false ) { // the value of '(cup.emptyCup)' is taken as true by default if we leave it as free/blank
12+ System .out .println ("The cup is NOT Empty" );
13+ }
14+ else {
15+ System .out .println ("The cup is Empty" );
16+ }
17+
18+ }
19+
20+
21+ }
22+
23+
Original file line number Diff line number Diff line change 1+ package learnConditionalStatemt ;
2+
3+ public class SwitchCaseExample {
4+
5+ String myHero = "Bat Man" ;
6+
7+ public void checkmyHero () {
8+
9+ switch (myHero ) {
10+ case "Iron Man" :
11+ System .out .println ("My hero is Iron Man" );
12+ break ; // If we dont give break; command, all other following cases will also get executed without checking the conditions
13+
14+ case "Super Man" :
15+ System .out .println ("My hero is Super Man" );
16+ break ;
17+
18+ case "Junk Man" :
19+ System .out .println ("My hero is Junk Man" );
20+ break ;
21+
22+ default :
23+ System .out .println ("My hero is Bat Man" );
24+
25+ }
26+
27+ }
28+
29+ public static void main (String [] args ) {
30+
31+ SwitchCaseExample hero = new SwitchCaseExample ();
32+ hero .checkmyHero ();
33+
34+
35+ }
36+
37+ }
Original file line number Diff line number Diff line change 33public class NoArgumentConstructor {
44
55 int empID ;
6- String empName ;
6+ String empPlace ;
7+ static String empName ;
78
89 // No argument or non-parametrized constructor example:
910 NoArgumentConstructor () { //No argument is passed to this constructor
1011 empID = 1234 ;
1112 empName = "Ajith" ;
1213 System .out .println ("No argument object is created: " +empID );
14+ System .out .println ("No argument object is created: " +empPlace );
15+
1316 }
1417
1518
1619 public static void main (String [] args ) {
1720 NoArgumentConstructor emp = new NoArgumentConstructor ();
21+ System .out .println ("No argument object is created: " +empName ); // can use the variable in main() method if it is static
1822 }
1923
2024}
Original file line number Diff line number Diff line change 1+ package learnLoopingConcepts ;
2+
3+ public class ForLoopExample {
4+
5+ public static void main (String [] args ) {
6+
7+ // Example for For loop
8+ for (int times =0 ; times <= 25 ; times ++) {
9+ System .out .println ("My Name is Ajith" );
10+ }
11+
12+
13+ // Example for While loop
14+ int i = 1 ;
15+ while (i <=25 ) {
16+ System .out .println ("Counting add by 1 is: " +i );
17+ i ++;
18+ }
19+
20+
21+ // Example for do - While loop
22+ int count = 0 ;
23+ do {
24+ System .out .println ("Verify and increase value by 1: " +count );
25+ count ++;
26+ }
27+ while (count <=25 );
28+
29+ }
30+
31+ }
Original file line number Diff line number Diff line change 1+ package staticMethodTypes ;
2+
3+ public class LearnStaticBlock {
4+
5+ static {
6+ System .out .println ("static block num 1" ); // All the static blocks will execute first before main() method
7+ }
8+
9+ static {
10+ System .out .println ("static block # 2" );
11+ }
12+
13+ static {
14+ System .out .println ("static block # 3" );
15+ }
16+
17+
18+ public static void main (String [] args ) {
19+ System .out .println ("main static method" ); // main method will execute after all static methods
20+
21+ }
22+
23+ }
Original file line number Diff line number Diff line change 1+ package staticMethodTypes ;
2+
3+ public class LearnStaticMethodExample {
4+
5+ public static void staticMethod () {
6+ // nonStatic(); // cannot call non-static methods to any other methods to execute it except main() method by creating/using object
7+ System .out .println ("It is Static Method" );
8+ }
9+
10+
11+ public void nonStatic () {
12+ staticMethod (); // can call a static method to any static or non-static methods simply without creating object
13+ System .out .println ("It is non-static method" );
14+ }
15+
16+ public static void main (String [] args ) {
17+ staticMethod (); //no need (not necessary) to create object for static methods
18+
19+ LearnStaticMethodExample stac = new LearnStaticMethodExample ();
20+ stac .nonStatic (); // need object to call non-static method(s)
21+
22+ System .out .println ("main method" );
23+ }
24+
25+ }
Original file line number Diff line number Diff line change 1+ package staticMethodTypes ;
2+
3+ public class LearnStaticVariable {
4+
5+ static int rows =50 ; // static variable name displays in italic font style
6+ static String name = "Name" ;
7+ int columns = 10 ;
8+ String table = "Table" ;
9+
10+ public static void main (String [] args ) {
11+
12+ rows =75 ;
13+ // columns=20; // cannot call the non-static variable without object
14+
15+ LearnStaticVariable object1 = new LearnStaticVariable ();
16+ object1 .rows =100 ;
17+ object1 .columns =25 ;
18+ object1 .table ="Table1" ;
19+ object1 .name ="Name1" ;
20+
21+ LearnStaticVariable object2 = new LearnStaticVariable ();
22+ object2 .rows =300 ;
23+ object2 .columns = 50 ;
24+ object2 .name ="Name2" ;
25+ object2 .table ="Table2" ;
26+
27+
28+ System .out .println ("object1 rows:" + object1 .rows ); //static variable is overridden by the final value and stored
29+ System .out .println ("object1 columns:" + object1 .columns ); // displays with normal given value
30+ System .out .println ("object1 table:" + object1 .table );
31+ System .out .println ("object1 name:" + object1 .name ); // static variable overridden and displays final value in o/p
32+
33+
34+ System .out .println ("object2 rows value is: " +object2 .rows );
35+ System .out .println ("object2 column value is: " +object2 .columns );
36+ System .out .println ("object2 name is: " +object2 .name );
37+ System .out .println ("object2 Table name is: " +object2 .table );
38+
39+ }
40+
41+ }
You can’t perform that action at this time.
0 commit comments