X Tutup
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Ron McLeod
Saloon Keepers:
  • Tim Holloway
Bartenders:

Understanding Java Basics – Variables, Data Types and Operators

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,

I am currently learning Java programming and would like to share some basic concepts that beginners should understand.

1. Variables
A variable is used to store data in a program. In Java, we must declare the variable with a data type.

Example:

int number = 10;
String name = "Sabari";

2. Data Types
Java has different types of data types such as:

int – used to store integers

double – used to store decimal numbers

char – used to store a single character

String – used to store text

Example:

int age = 20;
double price = 99.99;
char grade = 'A';
String city = "Madurai";

3. Operators
Operators are used to perform operations on variables.

Common operators:

Arithmetic: + - * /

Relational: == != > <

Logical: && ||

Example:

int a = 5;
int b = 3;
int sum = a + b;
System.out.println(sum);

Conclusion
Learning these basics helps beginners understand Java programming better before moving to advanced topics like loops, arrays, and object-oriented programming.

Thank you!
 
Marshal
Posts: 82157
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Madhumitha Moorthy wrote:. . . A variable is used to store data in a program. In Java, we must declare the variable with a data type.

Those are fields. Fields are used to store data in a program, but it is possible to store data other ways, e.g. writing them to files. There are also local variables, which have data for use in a method, but disappear when the method completes, Fields need to be declared with a specific type, but local variables can (and probably shoulm whenever possible) be declared with var.  Example:-

. . . Java has different types of data types  . . .

Nearly every class anybody created with the intention of instantiating that class becomes a datatype, so there are almost unlimited numbers of different datatypes. Those you named are only a few of the commonest types.
A double isn't a decimal fraction. It is a number probably with a fractional part, denominated in binary and occupying 64 bits. I don't have enough time to discuss the exact conventions in its format.

. . . Operators are used to perform operations on variables. . . .

Yes The operators && and || are called conditional operators in the Java® Language Specification (=JLS)←Link.

Learning these basics helps beginners understand Java programming better before moving to advanced topics like loops, arrays, and object-oriented programming. . . .

I have my own opinions, and many people will disagree with me. But I think learning what an object is, and what a field is, are fundamental to object‑oriented programming, and object‑oriented programming is more basic than loops and arrays, and should be taught first.
 
Master Rancher
Posts: 5291
87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell, I think the original poster may not have gotten to classes yet, in which case the distinction between local variables and fields isn't going to mean much.  I suspect the poster is talking about local variables, but will learn about var a little later, as a modification to what was already learned.  If I'm right, then saying "those are fields" will just cause confusion.
 
Campbell Ritchie
Marshal
Posts: 82157
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I meant that the things used to store information are called fields, not that what we were shown are fields.
 
Mike Simmons
Master Rancher
Posts: 5291
87
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A local variable stores information too - even if it may be more short-term than storage in a field.  There is still data being stored on the stack.  JLS 4.12 agrees:

4.12. Variables
A variable is a storage location and has an associated type, sometimes called its compile-time type, that is either a primitive type (§4.2) or a reference type (§4.3).

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic
X Tutup