0% found this document useful (0 votes)
25 views

CS 2nd Lec #04

Methods allow code to be

Uploaded by

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

CS 2nd Lec #04

Methods allow code to be

Uploaded by

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

Java Naming Convention

Java naming convention is a rule to follow as you decide what to name your
identifiers such as class, package, variable, constant, method, etc.

But, it is not forced to follow. So, it is known as convention not rule. These
conventions are suggested by several Java communities.

All the classes, interfaces, packages, methods and fields of Java programming
language are given according to the Java naming convention. If you fail to
follow these conventions, it may generate confusion or erroneous code.

The following table shows the popular conventions used for


the different identifiers.

Identifiers Naming Rules Examples


Type

Class It should start with the uppercase letter. public class Employee
It should be a noun such as Color, Button, System, {
Thread, etc. //code snippet
Use appropriate words, instead of acronyms. }

Interface It should start with the uppercase letter. interface Printable


It should be an adjective such as Runnable, Remote, {
ActionListener. //code snippet
Use appropriate words, instead of acronyms. }

Method It should start with lowercase letter. class Employee


It should be a verb such as main(), print(), println(). {
If the name contains multiple words, start it with a // method
lowercase letter followed by an uppercase letter such void draw()
as actionPerformed(). {
//code snippet
}
}

Variable It should start with a lowercase letter such as id, class Employee
name. {
It should not start with the special characters like & // variable
(ampersand), $ (dollar), _ (underscore). int id;
If the name contains multiple words, start it with the //code snippet
lowercase letter followed by an uppercase letter such }
as firstName, lastName.
Avoid using one-character variables such as x, y, z.

Package It should be a lowercase letter such as java, lang. //package


If the name contains multiple words, it should be package com.javatpoin
separated by dots (.) such as java.util, java.lang. class Employee
{
//code snippet
}

Constant It should be in uppercase letters such as RED, class Employee


YELLOW. {
If the name contains multiple words, it should be //constant
separated by an underscore(_) such as static fin
MAX_PRIORITY. int MIN_AGE = 18;
It may contain digits but not as the first letter. //code snippet
}

 Numeric Data Types and Operations:

 Numeric data types represent numerical values used in calculations.


 Common numeric data types in Java:

byte (8 bits): Stores small whole numbers (-128 to 127)


short (16 bits): Stores small whole numbers (-32,768 to 32,767)
int (32 bits): Most common integer type, good for whole numbers (-
2,147,483,648 to 2,147,483,647)
long (64 bits): Stores larger whole numbers
float (32 bits): Stores single-precision floating-point numbers (decimal
values)
double (64 bits): Stores double-precision floating-point numbers (more
precise than float)
 Numeric operations include:

Arithmetic (+, -, *, /, %)
Comparison (==, !=, <, >, <=, >=)
Increment (++, ++x) and Decrement (--, --x) (explained later)
 Numeric Literals:
 Numeric literals are the actual numbers you write in your code.
 They can be integers (e.g., 10, -25) or floating-point numbers (e.g.,
3.14, -12.5e2 which represents -12.5 * 10^2).
 Choose the appropriate data type based on the range and precision
required for your values.
 Evaluating Expressions and Operator Precedence:
 Expressions combine variables, literals, and operators to compute a
result.
 Operator precedence determines the order in which operations
within an expression are evaluated. (e.g., multiplication and division
happen before addition and subtraction)
 Use parentheses to override default precedence or clarify complex
expressions.

int result = 2 + 3 * 4; // Evaluates to 14 (multiplication happens


first)
int result = (2 + 3) * 4; // Evaluates to 20 (parentheses force
addition first)
 Augmented Assignment Operators:
 Augmented assignment operators combine assignment and an
operation in a single statement.
 They are shortcuts for common operations like:
+= (e.g., x += 5 is equivalent to x = x + 5)
-= (e.g., y -= 3 is equivalent to y = y - 3)
*=, /=, %= (similarly for multiplication, division, and modulo)
 Increment and Decrement Operators:
 Increment (++) and decrement (--) operators increase or decrease a
variable by 1.
 They can be used in two ways:
 Pre-increment/decrement (e.g., ++x):
In pre-increment/decrement, the operation is performed first,
then the result is used.

 Post-increment/decrement (e.g., x++, y--):


In post-increment/decrement, the current value is used first, then
the operation is performed.

Displaying the Current Time:


 You can use libraries in Java to access the current time.
 Here's an example using the java.util.Date class:

import java.util.Date;

public class CurrentTime {


public static void main(String[] args) {
Date now = new Date();
System.out.println("Current time: " + now); // Prints the current date
and time
}
}

Assignment:
 What is a method in Java?
 Method Declaration
 Naming a Method
 Types of Method
 Predefined Method
 User-defined Method

You might also like