From 196fd9dc9cf352214c84fbae8ab9d41e9d8b84f2 Mon Sep 17 00:00:00 2001 From: uvhareesh Date: Wed, 14 May 2025 18:27:34 +0530 Subject: [PATCH] Operators concept --- src/coreJava/AssignmentOperators.java | 34 ++++ src/coreJava/DataTypes.java | 220 +++++++++++++------------- src/coreJava/DecrementOperators.java | 39 +++++ src/coreJava/IncrementOperator.java | 40 +++++ src/coreJava/Operators.java | 67 ++++++++ src/coreJava/TernaryOperator.java | 28 ++++ 6 files changed, 318 insertions(+), 110 deletions(-) create mode 100644 src/coreJava/AssignmentOperators.java create mode 100644 src/coreJava/DecrementOperators.java create mode 100644 src/coreJava/IncrementOperator.java create mode 100644 src/coreJava/Operators.java create mode 100644 src/coreJava/TernaryOperator.java diff --git a/src/coreJava/AssignmentOperators.java b/src/coreJava/AssignmentOperators.java new file mode 100644 index 0000000..f8c11c7 --- /dev/null +++ b/src/coreJava/AssignmentOperators.java @@ -0,0 +1,34 @@ +package coreJava; + +public class AssignmentOperators { + + public static void main(String[] args) { + //5. Assignment = += -+ *= /= %= + + int a=10; + int b=10; + a+=5; //a=a+5; + b=b-5; //b=b-5; + System.out.println(a); + System.out.println(b); + + int c=10; + a*=2; //c=c*2; + System.out.println(c); + + int d=10; + d/=2; //d=d/2; + System.out.println(d); + + int e=10; + e%=2; //e=e%2; + System.out.println(e); + + /* difference b/w == = ? + * == is relational operators it compares the value + * = is assignment operator , it assigns the value + */ + + } + +} diff --git a/src/coreJava/DataTypes.java b/src/coreJava/DataTypes.java index 6f0e47c..101ae51 100644 --- a/src/coreJava/DataTypes.java +++ b/src/coreJava/DataTypes.java @@ -1,134 +1,134 @@ -package coreJava; - -public class DataTypes { - - public static void main(String[] args) { + package coreJava; - //variables - - //int s; //declaration - //s=100; //assignment - - int s=100; // declaration+assignment - System.out.println(s); - - s=200; - System.out.println(s); + public class DataTypes { + + public static void main(String[] args) { + //variables + + //int s; //declaration + //s=100; //assignment + + int s=100; // declaration+assignment + System.out.println(s); + + s=200; + System.out.println(s); + /* // approach1 // if all the variables belongs to different data types int p=1; int o=2; int i=3; */ - - /* approach2 // only if allthe variable are belongs to same data type + + /* approach2 // only if all the variable are belongs to same data type int p,o,i; p=1; o=2; i=3; */ - + // approach3 //only if all the variable are belongs to same data type - int p=1,o=2,i=3; - + int p=1,o=2,i=3; + //for meaningful message in print statement we can use concatenation method - System.out.println("The Value of p is :"+p); - System.out.println("The Value of o is :"+o); - System.out.println("The Value of i is :"+i); + System.out.println("The Value of p is :"+p); + System.out.println("The Value of o is :"+o); + System.out.println("The Value of i is :"+i); // in single line we printed all variable values - System.out.println(p+""+o+""+i+""); - + System.out.println(p+""+o+""+i+""); + // in print statement we used join method for each value with "" -//-------------------------------------------------------------------------- + //-------------------------------------------------------------------------- // Data types - - // Numeric Data Types - - int a=100, b=200; - System.out.println("The Value of a is :"+a); - System.out.println("The Value of b is :"+b); - System.out.println(a+b); - System.out.println("The sum of an and b is : "+(a+b)); - - byte by=125; - System.out.println(by); - - short sh=32767; - System.out.println(sh); - - long l=21212121234443534L; // literal is needed - // L should add, it can be lower or uppercase - System.out.println(l); - - // Decimal numbers - float, double - - float item_price=15.5f; // literal is needed - //f should add, it can be lower or uppercase - System.out.println(item_price); - - double dbl=1234.4343412; - System.out.println(dbl); - - // characters - - char grade='A'; // single character in single quotes - '' - System.out.println(grade); - - String name="Harish"; // String is non-premitive type and multiple characters with double quotes - "" - System.out.println(name); - - //char ch='abc'; //invalid - //Stirng ch='abc';//invalid - //String ch='A';//invalid - String ch="A"; //valid - System.out.println(ch); - - boolean bl=true; //allows only true or false - System.out.println(bl); - - boolean bl1=false;//allows only true or false - System.out.println(bl1); - - //boolean bl="true";//invalid - //boolean bl="false";//invalid - - //String bl=true;//invalid - String bl2="true";//valid - - //---------------------------------------------------------------------- - - //difference between variable and const/final - int x=100; - System.out.println(x); - x=200; - System.out.println(x); - - final int y=300; - System.out.println(y); - // y=400;// not allowed - //System.out.println(y); - - // variables declaration without final keyword we can change the values but wiht final keyword we cnat chaneg the value. in javascript instea dof final, const keyword is used. - - //java is statistically typed programming language - //python is dynamically typed programming language - //javascript is dynamically typed scripting language - - //Example : - - int q=100; - //x="welcome";//it is not allowed in java but in javascript and python it is allowed - //in python or javascript we no need to specify the data types explicitly. - - /* - r=100; - r="welcome"; - */ - //it is allowed in dynamically typed programming language - javascript / python + + // Numeric Data Types + + int a=100, b=200; + System.out.println("The Value of a is :"+a); + System.out.println("The Value of b is :"+b); + System.out.println(a+b); + System.out.println("The sum of an and b is : "+(a+b)); + + byte by=125; + System.out.println(by); + + short sh=32767; + System.out.println(sh); + + long l=21212121234443534L; // literal is needed + // L should add, it can be lower or uppercase + System.out.println(l); + + // Decimal numbers - float, double + + float item_price=15.5f; // literal is needed + //f should add, it can be lower or uppercase + System.out.println(item_price); + + double dbl=1234.4343412; + System.out.println(dbl); + + // characters + + char grade='A'; // single character in single quotes - '' + System.out.println(grade); + + String name="Harish"; // String is non-premitive type and multiple characters with double quotes - "" + System.out.println(name); + + //char ch='abc'; //invalid + //Stirng ch='abc';//invalid + //String ch='A';//invalid + String ch="A"; //valid + System.out.println(ch); + + boolean bl=true; //allows only true or false + System.out.println(bl); + + boolean bl1=false;//allows only true or false + System.out.println(bl1); + + //boolean bl="true";//invalid + //boolean bl="false";//invalid + + //String bl=true;//invalid + String bl2="true";//valid + +//---------------------------------------------------------------------- + + //difference between variable and const/final + int x=100; + System.out.println(x); + x=200; + System.out.println(x); + + final int y=300; + System.out.println(y); + // y=400;// not allowed + //System.out.println(y); + + // variables declaration without final keyword we can change the values but wiht final keyword we cnat chaneg the value. in javascript instea dof final, const keyword is used. + + //java is statistically typed programming language + // python is dynamically typed programming language + //javascript is dynamically typed scripting language + + //Example : + + int q=100; + //x="welcome";//it is not allowed in java but in javascript and python it is allowed + //in python or javascript we no need to specify the data types explicitly. + + /* + r=100; + r="welcome"; + */ + //it is allowed in dynamically typed programming language - javascript / python } diff --git a/src/coreJava/DecrementOperators.java b/src/coreJava/DecrementOperators.java new file mode 100644 index 0000000..7540ed5 --- /dev/null +++ b/src/coreJava/DecrementOperators.java @@ -0,0 +1,39 @@ +package coreJava; + +public class DecrementOperators { + + public static void main(String[] args) { + //5.Decrement operators -- + + //case1 + int a=10; + System.out.println(a); + + // a=a-1; + //instead of writing 2 times "a" we can use below code + a--; + System.out.println(a); + + //case2 post decrement + + int b=10; + int res=b--; + System.out.println(res); + + //expected o/p is 11 but we received 10 because first 10 is assigned to res then value is decrement with a++ + //first assignment is happen then decrement will happen + + System.out.println(a); + + //case3 Pre increment + + int c=10; + int res1=--c; + System.out.println(res1); + // first decrement the value then assignment is happen + + System.out.println(c); + + } + +} diff --git a/src/coreJava/IncrementOperator.java b/src/coreJava/IncrementOperator.java new file mode 100644 index 0000000..de2d49e --- /dev/null +++ b/src/coreJava/IncrementOperator.java @@ -0,0 +1,40 @@ +package coreJava; + +public class IncrementOperator { + + public static void main(String[] args) { + + //4.Increment & Decrement operators ++ -- + + //case1 + int a=10; + System.out.println(a); + + // a=a+1; + //instead of writing 2 times "a" we can use below code + a++; + System.out.println(a); + + //case2 post increment + + int b=10; + int res=b++; + System.out.println(res); + + //expected o/p is 11 but we received 10 because first 10 is assigned to res then value is incremented with a++ + //first assignment is happen then increment will happen + + System.out.println(a); + + //case3 Pre increment + + int c=10; + int res1=++c; + System.out.println(res1); + // first increment the value then assignment is happen + + System.out.println(c); + + } + +} diff --git a/src/coreJava/Operators.java b/src/coreJava/Operators.java new file mode 100644 index 0000000..0781f2b --- /dev/null +++ b/src/coreJava/Operators.java @@ -0,0 +1,67 @@ +package coreJava; + +public class Operators { + + public static void main(String[] args) { + + //1. Arithmetic operators +-*/% + // work on numeric type data only + + int a=20, b=10; + + int result=a+b; + + System.out.println("Sum of a and b:"+result); + System.out.println("Sub of a and b:"+(a-b)); + System.out.println("Multiply of a and b:"+(a*b)); + System.out.println("Div of a and b:"+(a/b)); + System.out.println("Percentage of a and b:"+(a%b)); + + //2. Relational/comparison operators > >= < <= != == + // return boolean value + //we can use all kinds data types + + System.out.println(a>b); + System.out.println(a=b); + System.out.println(a<=b); + + b=20; + + System.out.println(a>=b); + System.out.println(a<=b); + + System.out.println(a!=b); + System.out.println(a==b); + + boolean res=a>b; + System.out.println(res); + + //3.Logical operators && || ! + //returns boolean value - true/false + //works between 2 boolean values + + boolean x=true; + boolean y=false; + System.out.println(x && y);// false + System.out.println(x || y); //true + System.out.println(!x);//true + System.out.println(!y);//false + + + boolean b1=10>20; + System.out.println(b1);//false + + boolean b2=20>10; + System.out.println(b2);//true + + System.out.println(b1 && b2);//false + System.out.println(b1 || b2);//true + + //combination of relational and logical operators + + System.out.println((10<20) && (20>10));//true + + } + +} diff --git a/src/coreJava/TernaryOperator.java b/src/coreJava/TernaryOperator.java new file mode 100644 index 0000000..c2efb24 --- /dev/null +++ b/src/coreJava/TernaryOperator.java @@ -0,0 +1,28 @@ +package coreJava; + +public class TernaryOperator { + + public static void main(String[] args) { + + //Syntax: var=exp ? result1 : result2; + + //ex1: + int a=200, b=100; + int x=(a>b)? a:b; + System.out.println(x); + + //ex2: + int y=(1==1)?100:200; + System.out.println(y); + + //ex3: + int z=(1==2)?100:200; + System.out.println(z); + + //ex4: + int person_age=30; + String res=(person_age>=18)?"Eligible":"Not Eligible"; + System.out.println(res); + } + +}