chapter 1- variables and datatypes

 Just like we have some rules that we follow to speak English(grammar), we have some rules to follow while writing a java program. The set of these rules is called syntax(vocabulary and grammar of java). 

Variables

 The variable is a container that stores a value. This value can be changed during the execution of the program.

Example: int number = 8;

here, int is a data type, the number is the variable name and 8 is the value it stores 

Rules for declaring a variable name

we can choose a name by declaring a java variable

if the following rules are followed:

1. Must not begin with a number ==>int 1arry; is invalid!

2. Name is case sensitive ==>dhruv and Dhruv are different!

3. Should not be a keyword( like void).

4. white space is not allowed. ==>int developer dhruv is invalid.

5. Can contain alphabets, $ character _ character, and digits if the other conditions are met.


Data types

Data types in java fall under the following categories.

1.Primitive data types (intrinsic)

2.Non-primitive data types(derived)


Primitive Data Types

Java is statically typed.==.variables must be declared before use!

there are 8 types of primitive data types supported by java:

1.BYTE - valve ranges from -128 to 127 

                takes 1 byte

               default value is zero

2.SHORT - value ranges from-(2^16) to (2^16)-1

                   takes 2 bytes

                  default value is 0

3.INT - value ranges from-(2^32) to (2^32)-1

            takes 4 bytes

            default value is 0

4.FLOAT - value ranges from (see docs) 

                  takes 4 bytes

                  default value is 0.0f

5.LONG - value ranges from -(2^64) to (2^64)-1

                 takes 8 bytes

                default value is 0

6.DOUBLE - value ranges from (see docs)

                      takes 8 bytes 

                      default value is 0.0d

7.CHAR - value ranges from 0 to 65535(2^16-1)

                 takes 2 bytes ==>because it supports Unicode 

                 default value is '100000'

8.BOOLEAN-value can be true or false

                       size depends on JVM 

                      default value is false


QUICK QUIZ: write a java program to add 3 numbers.



                                                          







Comments

Popular posts from this blog

Introduction to Java