0% found this document useful (0 votes)
9 views7 pages

Java Comments, Data Types, and Variables

The document provides an overview of Java comments, data types, and variables. It explains the two types of comments (single-line and multi-line), outlines primitive and non-primitive data types, and describes local, instance, and class/static variables. Additionally, it includes examples and exercises related to these concepts.

Uploaded by

mitesh.parishkar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

Java Comments, Data Types, and Variables

The document provides an overview of Java comments, data types, and variables. It explains the two types of comments (single-line and multi-line), outlines primitive and non-primitive data types, and describes local, instance, and class/static variables. Additionally, it includes examples and exercises related to these concepts.

Uploaded by

mitesh.parishkar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Comments

Comments in any programming language are ignored by the compiler or the interpreter. A
comment is a part of the coding file that the programmer does not want to execute; rather, the
programmer uses it to either explain a block of code or to avoid the execution of a specific part of
code while testing.

There are two types of comments:

• Single-line comment

• Multi-line comment

A. Single Line Comments:

To write a single-line comment, just add a // at the start of the line.

Example:

public class Details {

public static void main(String[] args) {

// This is a single line comment

[Link]("Hello World!!!");

Output:

Hello World!!!

B. Multi Line Comments:

To write a multi-line comment, just add a /*...*/ at the start and end of the comment block.

Example:

public class Details {

public static void main(String[] args) {

/* This

* is

*a

* Multiline

* Comment

*/

[Link]("Hello World!!!");

Output:

Hello World!!!
Java Data Types

There are two forms of datatypes in Java:

• Primitive data type

• Non-Primitive data type

• bool: Boolean data type consists of true and false values.

• char: char datatype is used to store characters.

• byte: The main purpose of byte is to save memory and consists of values in the range -128
to 127.

• short: Consists of values in the range -32768 to 32767.

• int: Consists of values in the range -2,147,483,648 to 2,147,483,647.

• long: Consists of values in the range -9,223,372,036,854,775,808 to


9,223,372,036,854,775,807.

• float: Can be used to deal with decimal numbers. Always recommended to use float rather
than double because float saves memory.

• double: Can be used to deal with decimal numbers.

Data Type Size Range


bool 1 bit true, false
char 2 byte a…z, A…Z
byte 1 byte -27 to 27-1 (-128 to 127)
short 2 byte -215 to 215-1 (-32768 to 32767)
int 4 byte -231 to 231-1 (-2,147,483,648 to 2,147,483,647)
-263 to 263-1 (-9,223,372,036,854,775,808 to
long 8 byte
9,223,372,036,854,775,807)
float 4 byte 6.022f
double 8 byte 3.142
Java Variables

Variables are containers that store information that can be manipulated and referenced later by the
programmer within the code. Java variables have a data type associated with them which can tell
us about the size and layout of the variable’s memory.

Syntax:

datatype variable = value

There are three types of variables in Java:

• Local variable

• Instance variable

• Class/Static variable

A. Local Variables:

A variable that is declared inside the body of the method or constructor is called a local variable. It
is called so because the extent of a local variable lies only within the method or constructor within
which it is created and other methods in the class cannot access it.

Inside the method body, a local variable is declared using the static keyword.

Example:

public class variableType {

public void localVariable() {

String name = "Ben";

int marks = 95;

[Link](name + " Scored " + marks + "%.");

public static void main(String[] args) {

variableType vt = new variableType();

[Link]();

Output:

Ben Scored 95%.


If it is accessed outside the method or constructor within which it is created, then it gives an
error.

Example:

public class variableType {

public void localVariable() {

String name = "Ben";

int marks = 95;

public void notLocalVariable() {

[Link](name + " Scored " + marks + "%.");

public static void main(String[] args) {

variableType vt = new variableType();

[Link]();

Output:

name cannot be resolved to a variablemarks cannot be resolved to a variable

B. Instance Variables:

An instance variable is declared inside the class but outside a method or a constructor. It is similar
to a static variable except that it is declared without using the keyword static. These variables are
accessible by all methods or constructors that are inside the class.

Example:

public class variableType {

public String name = "Ben";

public int marks = 95;

public void instanceVariable() {

[Link](name + " Scored " + marks + "%.");

public static void main(String[] args) {

variableType vt = new variableType();

[Link]();

Output: Ben Scored 95%.


C. Class/Static Variables:

A static variable is declared inside the class but outside a method or a constructor. It is similar to an
instance variable except that it is declared using the keyword static. These variables are accessible
by all methods or constructors that are inside the class.

Example:

public class variableType {

public static String name;

public static int marks;

public static void main(String[] args) {

name = "Ben";

marks = 95;

[Link](name + " Scored " + marks + "%.");

Output:

Ben Scored 95%.

If a variable is not declared static then it gives an error.

Example:

public class variableType {

public String name;

public int marks;

public static void main(String[] args) {

name = "Ben";

marks = 95;

[Link](name + " Scored " + marks + "%.");

Output:

Cannot make a static reference to a non-static field


Parishkar College of Global Excellence (Autonomous)
BCA V Sem
Minor - Java programming
Comment, Data Types, Variables

Name: __________________________________ ID: _______________ Date: ________________

Part 1: Java Comments


1. What is the main purpose of comments in programming?

2. List the two types of comments in Java.


a) _________________________
b) _________________________
3. How do you create a single-line comment in Java? Provide an example.

4. How do you create a multi-line comment in Java? Provide an example.

5. True or False: Comments are executed by the compiler or interpreter.


Part 2: Java Data Types
1. What are the two main forms of data types in Java?
a) _________________________
b) _________________________
2. Categorize the following data types as either Primitive or Non-Primitive:
• int: _________________________ • Array: _________________________
• String: _________________________ • boolean: _________________________
• char: _________________________ • Classes: ________________________
3. Fill in the Blanks with the correct data type:
• To store true or false values, use the _________ data type.
• To store a single character, use the _________ data type.
• For saving memory with integer values between -128 and 127, use the _________ data type.
• For large whole numbers up to
263−1, use the _________ data type.
• When dealing with decimal numbers and needing to save memory, it's recommended to use the
_________ data type over double.
4. Match the data type to its size (according to the lecture):

Data Type Size (in bytes/bits) Data Type Size (in bytes/bits)

char long
int float
byte double
boolean short

Choices: 1 bit , 1 byte , 2 bytes , 4 bytes , 8 bytes

Part 3: Java Variables


1. What is the purpose of variables in Java programming?
2. What is the general syntax for declaring and initializing a variable in Java?

3. List the three types of variables in Java.


a) _________________________
b) _________________________
c) _________________________
4. Define a Local Variable. Where can it be accessed?

5. Consider the following code snippet. Why does the [Link] line in notLocalVariable()
cause an error?
public class variableType {
public void localVariable() {
String name = "Ben";
int marks = 95;
}
public void notLocalVariable() {
[Link](name + " Scored " + marks + "%."); // This line causes an error
}
public static void main(String[] args) {
variableType vt = new variableType();
[Link]();
}
}
Explanation:

6. Define an Instance Variable. Where is it declared and by which methods can it be accessed?

7. Define a Class/Static Variable. How is it declared differently from an instance variable, and where can
it be accessed?

8. What happens if you try to access a non-static variable directly from a static main method without an
object instance?

You might also like