logotransparent.png

Legion of Learners

www.lol-101.com

  • Home

  • Classrooms

  • Courses

  • About Us

    • Executive Team
    • Board Members
  • Resources

  • More

    Use tab to navigate through the menu items.
    To see this working, head to your live site.
    • Categories
    • All Posts
    • My Posts
    sfolax6776
    Sep 20, 2019
      ·  Edited: May 15, 2020

    2.2 How to declare and initialize variables for computation?

    in Introduction to Java

    A. Introduction:

    Other than computing with literals, a program often stores values for output or further computations. We store values in variables. Because there are different types of values such as int, double or strings, variables have different types to match the types of values that they store. This is why when "creating" a new variable, we need to declare the type of this variable and sometimes, give it an initial value.


    B. Let's turn the Phone Charge algorithm in 1.6 into a program. First we need to declare and initialize three variables:

    The above declares 3 variables and immediately give them initial values. This is recommended because it is concise and clear. In the case that you do not know the initial values, you can declare variables first and initialize them later.


    Keep in mind that a variable can be declared ONLY ONCE. It can be set to new values as often as needed.


    C. Try: R4.27 and R4.28 of this

    There are 8 primitive types in Java: int, short, long, byte, float, double, char, boolean . Their descriptions below might help you to answer R4.27 and R4.28 above. Can you declare a variable of each type and give proper initial values?



    0 comments
    0
    0 comments

    Questions? Email us at legionoflearners@gmail.com or join our WeChat group!

    • lol-101dotcom

    ©2020 Legion of Learners