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

Java Naming conventions

The document outlines Java naming conventions, which are guidelines for naming identifiers like classes, methods, and variables to enhance code readability. It also explains command-line arguments in Java, providing examples of how to receive and print these arguments in a Java program. Additionally, it describes how to create threads in Java by extending the Thread class or implementing the Runnable interface, detailing the methods and constructors associated with thread management.

Uploaded by

narendranvel
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)
7 views9 pages

Java Naming conventions

The document outlines Java naming conventions, which are guidelines for naming identifiers like classes, methods, and variables to enhance code readability. It also explains command-line arguments in Java, providing examples of how to receive and print these arguments in a Java program. Additionally, it describes how to create threads in Java by extending the Thread class or implementing the Runnable interface, detailing the methods and constructors associated with thread management.

Uploaded by

narendranvel
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/ 9

Java Naming conventions

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.

All the classes, interfaces, packages, methods and fields of java programming language are given
according to java naming convention.

Advantage of naming conventions in java


By using standard Java naming conventions, you make your code easier to read for yourself and
for other programmers. Readability of Java program is very important. It indicates that less time
is spent to figure out what the code does.

Name Convention
should start with uppercase letter and be a noun e.g. String, Color, Button, System,
class name
Thread etc.
interface should start with uppercase letter and be an adjective e.g. Runnable, Remote,
name ActionListener etc.
should start with lowercase letter and be a verb e.g. actionPerformed(), main(),
method name
print(), println() etc.
variable name should start with lowercase letter e.g. firstName, orderNumber etc.
package name should be in lowercase letter e.g. java, lang, sql, util etc.
constants
should be in uppercase letter. e.g. RED, YELLOW, MAX_PRIORITY etc.
name

CamelCase in java naming conventions


Java follows camelcase syntax for naming the class, interface, method and variable.

If name is combined with two words, second word will start with uppercase letter always e.g.
actionPerformed(), firstName, ActionEvent, ActionListener etc.

Java Command Line Arguments

Command Line Argument


Simple example of command-line argument
Example of command-line argument that prints all the values

The java command-line argument is an argument i.e. passed at the time of running the java program.
The arguments passed from the console can be received in the java program and it can be used as an
input.

So, it provides a convenient way to check the behavior of the program for the different values. You can
pass N (1,2,3 and so on) numbers of arguments from the command prompt.
Simple example of command-line argument in java
In this example, we are receiving only one argument and printing it. To run this java program, you must
pass at least one argument from the command prompt.

class CommandLineExample{
public static void main(String args[]){
System.out.println("Your first argument is: "+args[0]);
}
}

compile by > javac CommandLineExample.java


run by > java CommandLineExample sonoo

Output: Your first argument is: sonoo

Example of command-line argument that prints all the values


In this example, we are printing all the arguments passed from the command-line. For this purpose, we
have traversed the array using for loop.

class A{
public static void main(String args[]){

for(int i=0;i<args.length;i++)
System.out.println(args[i]);

}
}

compile by > javac A.java


run by > java A sonoo jaiswal 1 3 abc

Output: sonoo
jaiswal
1
3
abc

Contact Us | Contribute | Ask Question | login


Subscribe Us

91-9990449935
0120-4256464

 Home
 Core Java
 Servlet
 JSP
 EJB
 Struts2
 Mail
 Hibernate
 Spring
 Android
 Design P
 Quiz
 Projects
 Interview Q
 Comment
 Forum
 Training

Basics of Java OOPs Concepts Java String Java Regex Exception Handling Java Inner classes

Java Multithreading
What is Multithreading Life Cycle of a Thread Creating Thread Thread Scheduler Sleeping a thread Start
a thread twice Calling run() method Joining a thread Naming a thread Thread Priority Daemon Thread
Thread Pool Thread Group ShutdownHook Performing multiple task Garbage Collection Runtime class
Multithreading quiz-1 Multithreading quiz-2

Java Synchronization
Synchronization in java synchronized block static synchronization Deadlock in Java Inter-thread Comm
Interrupting Thread Reentrant Monitor

Java I/O Java Networking Java AWT Java Swing Java Applet Java Reflection Java Date Java Conversion
Java Collection Java JDBC Java New Features RMI Internationalization Interview Questions

next>> <<prev

How to create thread


There are two ways to create a thread:

1. By extending Thread class


2. By implementing Runnable interface.

Thread class:
Thread class provide constructors and methods to create and perform operations on a thread.Thread
class extends Object class and implements Runnable interface.

Commonly used Constructors of Thread class:

 Thread()
 Thread(String name)
 Thread(Runnable r)
 Thread(Runnable r,String name)

Commonly used methods of Thread class:

1. public void run(): is used to perform action for a thread.


2. public void start(): starts the execution of the thread.JVM calls the run() method on the thread.
3. public void sleep(long miliseconds): Causes the currently executing thread to sleep
(temporarily cease execution) for the specified number of milliseconds.
4. public void join(): waits for a thread to die.
5. public void join(long miliseconds): waits for a thread to die for the specified miliseconds.
6. public int getPriority(): returns the priority of the thread.
7. public int setPriority(int priority): changes the priority of the thread.
8. public String getName(): returns the name of the thread.
9. public void setName(String name): changes the name of the thread.
10. public Thread currentThread(): returns the reference of currently executing thread.
11. public int getId(): returns the id of the thread.
12. public Thread.State getState(): returns the state of the thread.
13. public boolean isAlive(): tests if the thread is alive.
14. public void yield(): causes the currently executing thread object to temporarily pause and allow
other threads to execute.
15. public void suspend(): is used to suspend the thread(depricated).
16. public void resume(): is used to resume the suspended thread(depricated).
17. public void stop(): is used to stop the thread(depricated).
18. public boolean isDaemon(): tests if the thread is a daemon thread.
19. public void setDaemon(boolean b): marks the thread as daemon or user thread.
20. public void interrupt(): interrupts the thread.
21. public boolean isInterrupted(): tests if the thread has been interrupted.
22. public static boolean interrupted(): tests if the current thread has been interrupted.

Runnable interface:
The Runnable interface should be implemented by any class whose instances are intended to be
executed by a thread. Runnable interface have only one method named run().

1. public void run(): is used to perform action for a thread.

Starting a thread:
start() method of Thread class is used to start a newly created thread. It performs following tasks:

 A new thread starts(with new callstack).


 The thread moves from New state to the Runnable state.
 When the thread gets a chance to execute, its target run() method will run.

1)By extending Thread class:

1. class Multi extends Thread{


2. public void run(){
3. System.out.println("thread is running...");
4. }
5. public static void main(String args[]){
6. Multi t1=new Multi();
7. t1.start();
8. }
9. }

Output:thread is running...

Who makes your class object as thread object?


Thread class constructor allocates a new thread object.When you create object of Multi class,your class
constructor is invoked(provided by Compiler) fromwhere Thread class constructor is invoked(by super()
as first statement).So your Multi class object is thread object now.

2)By implementing the Runnable interface:

1. class Multi3 implements Runnable{


2. public void run(){
3. System.out.println("thread is running...");
4. }
5.
6. public static void main(String args[]){
7. Multi3 m1=new Multi3();
8. Thread t1 =new Thread(m1);
9. t1.start();
10. }
11. }

Output:thread is running...
If you are not extending the Thread class,your class object would not be treated as a thread object.So
you need to explicitely create Thread class object.We are passing the object of your class that
implements Runnable so that your class run() method may execute.

Next TopicThread Schedular

<<prev next>>
Like/Subscribe us for latest updates or newsletter ↑Top

Tutorials

 » Core Java Tutorial


 » Servlet Tutorial
 » JSP Tutorial
 » Mail API Tutorial
 » Design Pattern Tutorial
 » Struts Tutorial
 » Spring Tutorial
 » Hibernate Tutorial
 » Android Tutorial
 » JavaScript Tutorial
 » SQL Tutorial
 » C Tutorial
 » AJAX Tutorial
 » JUnit Tutorial
 » JAXB Tutorial
 » Maven Tutorial

Interview Questions

 » Java Interview
 » Servlet Interview
 » JSP Interview
 » Hibernate Interview
 » Spring Interview
 » Android Interview
 » SQL Interview
 » PL/SQL Interview
 » Oracle Interview
 » MySQL Interview
 » SQL Server Interview
 » MongoDB Interview
 » Cloud Interview

Quizzes

 » Core Java quiz


 » Servlet quiz
 » JSP quiz
 » Struts2 quiz
 » Android quiz
 » OCJP quiz
 » OCWCD quiz
 » Hibernate quiz
 » Spring quiz
 » C quiz
 » Cloud Computing quiz
 » JavaScript quiz
 » SQL quiz

Forum

 » Core Java Ques.


 » Servlet Ques.
 » JSP Ques.
 » Struts Ques.
 » Spring Ques.
 » Hibernate Ques.
 » Android Ques.

Projects

SSS IT PVT LTD

 » Development
 » Training
 » SEO
 » Consultancy

CONTACT US
Tel. : 0120-4256464
Mob. : +91 9990449935
Email : [email protected]
Address: 2nd Floor, G-13,
(Near 16 Metro Station),
Sec - 3, Noida,
201301, UP, India
» Contact Us
» Privacy Policy

© 2011-2014 Javatpoint.
All Rights Reserved.

You might also like