Day 5 Of Our Month Of Java

in #java6 years ago

Data Types And Operators

ai-blur-codes-577585.jpg
Image courtesy of pexels.com

I am looking ahead and not sure where my “Month of Java” is going to take me. I look at code that people are working on and think these small steps are just so far away from where I need to be. I’ll persevere though and see where this month takes me. I may need to look at changing my approach sooner or later.

What Did I Learn Today

  • Data Types: A Data type defines the values a variable can take, eg; and int data type can only take integer values. The data type needs to be known at compile time, with two categories of data types available in Java:
  1. Primitive data types: There are 8 primitive data types which are boolean, char, byte, short, int, long, float and double. The size of these data types do not change from one operating system to another where byte, short, int and long data types are used for storing whole numbers. Float and double are used for fractional numbers and char is used for storing characters(letters). Boolean data type is used for variables that holds either true or false.
  2. Non primitive Data Types: Arrays and Strings are non-primitive data types.
  • Operators: Operators represent an action for example; Basic arithmetic operators are: +, -, , /, %
    Assignments operators in java are: =, +=, -=, =, /=, %=
    num2 = num1 would assign value of variable num1 to the variable.
    num2+=num1 is equal to num2 = num2+num1
    num2-=num1 is equal to num2 = num2-num1
    num2
    =num1 is equal to num2 = num2
    num1
    num2/=num1 is equal to num2 = num2/num1
    num2%=num1 is equal to num2 = num2%num1

  • Auto-increment and Auto-decrement Operators
    ++ and —
    num++ is equivalent to num=num+1;
    num–- is equivalent to num=num-1;

  • Logical Operators
    Logical Operators are used with binary variables. They are mainly used in conditional statements and loops for evaluating a condition.
    Logical operators in java are:
    && - and
    || - or
    ! - not

  • Comparison(Relational) operators
    We have six relational operators in Java:
    "==" equal to
    "!=" not equal to
    ">" less than
    "<" greater than
    ">=" less than or equal to
    "<=" greater than or equal to

When it comes to assignment operators, we will be seeing in the next few days where we start to use “if statements” and “loops”. We use these assignment comparison operators to determine if a condition is met, for example; if variable1 “is equal to” variable2.

Code For The Day


Let's test out our Arithmetic Operators in the following code example:

  1 public class ArithmeticOperatorDemo {
  2         public static void main(String args[]) {
  3                 int num1 = 100;
  4                 int num2 = 20;
  5 
  6                 System.out.println("num1 + num2: " + (num1 + num2));
  7                 System.out.println("num1 - num2: " + (num1 - num2));
  8                 System.out.println("num1 * num2: " + (num1 * num2));
  9                 System.out.println("num1 / num2: " + (num1 / num2));
 10                 System.out.println("num1 % num2: " + (num1 % num2));
 11         }
 12 }

Output

java ArithmeticOperatorDemo 
num1 + num2: 40
num1 - num2: 0
num1 * num2: 400
num1 / num2: 1
num1 % num2: 0

I'll be posting daily, sharing my experiences on my “1 Month of Java Code” experiences”, my previous post on day 4 can be found below, so feel free to have a look:
https://steemit.com/java/@run.vince.run/java-variables-for-day-4-of-30-days-of-java

If you have found this post useful or interesting, please consider Commenting, Upvoting, Following and/or Resteeming

Sort:  

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by run.vince.run from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.

Coin Marketplace

STEEM 0.20
TRX 0.15
JST 0.029
BTC 64401.36
ETH 2627.01
USDT 1.00
SBD 2.83