DEV Community

Janardhan Pulivarthi
Janardhan Pulivarthi

Posted on • Updated on

Day 4 of 100 - Java: Data Types

Binge watching, Java Basics

String concatenation:

today = 4;
total = 100;
System.out.println("Day "+today+" of "+total+" - Learning Java");
Enter fullscreen mode Exit fullscreen mode

Points:

  1. Use lower camel case for variable names.
  2. use full names. speed instead of sp;
  3. Start variable with a letter. var8 or v1 and not 8var
  4. Long or Integer for integer values. Double for fractions
  5. String or Character datatypes. Character cannot be used for calculations

    String text = "(a) Today is January 12 2022";
    char hello = '4';
    char three = '3';
    //char ten = '10'; // Cannot store more than one letter.
    
  6. boolean

    //boolean hello = 21; //incorrect
    boolean hello = true;
    // long number = 100.1; // only natural numbers
    
  7. Variable Arithmetic

    int negNumber = -20; // =-20
    int multNumber = 5*6; // = 30
    //
    double divide = 5/2; //=2
    double divide2 = 24/5; //not 4.8, =4
    double accurateDivide = 5/2.0; // 2.5
    //
    int x = 2+3; //5
    int y = 4-5; //-1
    int z = x*y; //-5
    //
    double paid = 10;
    double change = 3.25;
    double tip = (paid-change)*0.2;
    // double tip = paid - change * 0.2;
    
  8. If in doubt, use parentheses (). * or / before + or -.

  9. Casting

    double div = 5/2;
    System.out.println(div);//2
    double accurateDiv = (double)x/y;
    System.out.println(accurateDiv);
    

Top comments (0)