0% found this document useful (0 votes)
75 views9 pages

Assignment 1: CS 175 Programming in Java

This document discusses variables and operators in Java. It notes that variables store information and are declared with a data type. There are 8 basic data types in Java including 4 for integers (byte, short, int, long), and 4 for decimals/characters (float, double, char, boolean). The document provides examples of initializing variables of each data type and explains naming conventions and rules for variables in Java. It also covers basic operators like assignment, addition, subtraction, multiplication, division, and modulus.

Uploaded by

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

Assignment 1: CS 175 Programming in Java

This document discusses variables and operators in Java. It notes that variables store information and are declared with a data type. There are 8 basic data types in Java including 4 for integers (byte, short, int, long), and 4 for decimals/characters (float, double, char, boolean). The document provides examples of initializing variables of each data type and explains naming conventions and rules for variables in Java. It also covers basic operators like assignment, addition, subtraction, multiplication, division, and modulus.

Uploaded by

driftwood 069
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

ASSIGNMENT 1

CS 175 Programming in Java


Variables and Operators

• variable are used to store information to be referenced and


manipulated in a computer program
• Variables are first stated with a data type
• Operator is a symbol that tells a compiler or interpreter to perform a
specific mathematical ,relational or logical operation and produce a
final result
• There are 8 basic data types in java
• Where 4 of the basic data types are for integers
Data types
• 1. byte it is used to store integers from -128 to 127 example byte
n0fchildren
• As we expect there is no parent with more than 127 children
• 2.short is an integer data type representing 2 bytes integers
• 3.int is a short form of integer ,it is a data type to represent 4 bytes
integers
• 4.long is an integer data type representing 8 bytes integers
• 5.float it is a data type for decimal point numbers with 4bytes
• 6.double is a data type for decimal point numbers with 8bytes
• 7.char it is used to represent characters example @,#,&,*
• 8.Boolean it is a data type that hold either true or false
Rules for naming a variable in java
• A variable can contain letters, numbers, underscores(_) or $ sign
• First character of a variable can not be a number
• A variable should be short but meaningful
• A variable should not be a word that is present in java library example
while, for, if
• It is not must , but it is advised to use camel casing example
( thisIsVariable )
Initializing a variable
• byte userAge = 20;
• 2 short numberOfStudents = 45;
• 3 int numberOfEmployees = 500;
• 4 long numberOfInhabitants = 21021313012678L;
• 5 float hourlyRate = 60.5F;
• 6 double numberOfHours = 5120.5;
• 7 char grade = 'A’;
• 10 boolean promote = true;
• 11 byte level = 2,
• 12 userExperience = 5;
Explaining naming of variables
• float hourlyRate = 60.5F;
• There is a suffix “F”. This is because by default, any floating point number is
treated as a double by Java. We need to add the suffix “F” to indicate to the
compiler that hourlyRate is of float data type.

• long numberOfInhabitants = 21021313012678L;


• Suffix L is added to speciy that integer is Long
• suffix “L” to the end of the number. Hence, on line 4 when we initialized
numberOfInhabitants, we added “L” to the end of the number. If we do not do
that, the compiler will complain that the number is too large and give us an
error.
Variable continue…..
• You can also declare a variable then initialize a certain value to it

• Example
• Int year ;
• Year =2021 ;
OPERATORS
• Assignment operator
• = is an assignment operator , different from math where = means equal , in
programming x=y and y=x are different
• Example
• Int x=4 ;
• Int y=5 ;
• X=y ;
• Then x becomes 5
• Y=x ;
• Then y becomes 4
BASIC OPERATORS
• Addition: x + y = 9
• Subtraction: x - y = 5
• Multiplication: x*y = 14
• Division: x/y = 3 (rounds down the answer to the nearest integer)
• Modulus: x%y = 1 (gives the remainder when 7 is divided by 2)
• IN java the result is integer if operands are both integers otherwise it
is not an integer

You might also like