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

Java Naming Conventions Explained

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

Java Naming Conventions Explained

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

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[]){
[Link]("Your first argument is: "+args[0]);
}
}

compile by > javac [Link]


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<[Link];i++)
[Link](args[i]);

}
}

compile by > javac [Link]


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 [Link]
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 [Link] 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 [Link] 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. [Link]("thread is running...");
4. }
5. public static void main(String args[]){
6. Multi t1=new Multi();
7. [Link]();
8. }
9. }

Output:thread is running...

Who makes your class object as thread object?


Thread class constructor allocates a new thread [Link] 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. [Link]("thread is running...");
4. }
5.
6. public static void main(String args[]){
7. Multi3 m1=new Multi3();
8. Thread t1 =new Thread(m1);
9. [Link]();
10. }
11. }

Output:thread is running...
If you are not extending the Thread class,your class object would not be treated as a thread [Link]
you need to explicitely create Thread class [Link] 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 : enquiry@[Link]
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.

Common questions

Powered by AI

The 'Runnable' interface in Java is significant because it defines the `run()` method that is meant to include code executed by a thread, promoting a cleaner separation between thread logic and running mechanics. Implementing `Runnable` enhances Java's thread handling capabilities by allowing a class to implement multiple behaviors beyond threading (since a class that implements `Runnable` can still extend other classes), thus facilitating more flexible and reusable designs in multithreaded applications .

Deprecated methods for thread management in Java include `suspend()`, `resume()`, and `stop()`. Their use is discouraged due to potential for deadlock, inconsistency, or system state corruption. For example, `suspend()` and `resume()` can lead to deadlock as threads can be suspended indefinitely. Similarly, `stop()` may terminate a thread abruptly without allowing it to release resources properly or complete its tasks, leading to inconsistent program states .

In Java, constants are typically named using uppercase letters, with words separated by underscores, such as RED, YELLOW, or MAX_PRIORITY. This convention is designed to distinguish constants from variables, as the uppercase lettering signals that the value is not meant to change throughout the program, thereby enhancing code readability and intention clarity .

Marking a thread as a daemon thread in Java indicates that it is a background service thread that should not prevent the JVM from exiting when the program finishes execution. Daemon threads are used for tasks such as garbage collection or housekeeping. Once all user threads (non-daemon) are completed, the JVM can exit, even if daemon threads are still running. This affects program execution by potentially terminating daemon tasks prematurely without completing their execution, which necessitates careful resource management to prevent resource leaks or inconsistent states .

Following Java naming conventions is important because it enhances the readability of the code, making it easier for yourself and other programmers to understand. This improves code maintainability and decreases the time spent figuring out what the code does. By adhering to these standards, the overall quality and consistency of the codebase are also improved .

Passing command-line arguments is important in Java programs as it allows users to influence the program's behavior dynamically at runtime without needing to alter the source code. This feature provides flexibility for testing and production environments, allowing different inputs to be tested easily. It is implemented by passing arguments to the `main` method as an array of `String` objects, which the program then interprets and uses as needed .

Java's `start()` method initiates a thread's execution by causing the JVM to call the `run()` method of the given thread, transitioning the thread from the new state to runnable and eventually to running. This method creates a new call stack for the thread, allowing it to run concurrently with other threads. In contrast, directly calling the `run()` method does not start a new thread; instead, it executes in the current thread's stack, maintaining single-threaded behavior and bypassing the thread life cycle .

Threads in Java can be created by either extending the `Thread` class or implementing the `Runnable` interface. Extending `Thread` allows simple inheritance and direct control over thread methods, which can be useful for quick, short-lived tasks. However, Java does not support multiple inheritance, limiting flexibility. Implementing `Runnable`, on the other hand, promotes separation of thread logic and execution while allowing the class to extend other classes. This approach is preferable for applications with complex business logic where classes need to serve multiple roles. Selecting between these depends on the design needs and inheritance requirements of the application .

Using verbs for method names in Java's naming conventions is preferred because it clearly indicates that the method is performing an action. This improves the readability and intuitive understanding of what the method seeks to accomplish. For instance, method names like `print()`, `calculateTotal()`, or `isValid()` clearly convey the action or logic that will be applied, making it easier for developers to infer function and intent without needing to delve into the implementation details .

Java's camelCase naming convention for methods and variables affects code interpretation by clearly distinguishing the start of new words within an identifier, which increases the readability of the code. For methods, camelCase indicates actions to be performed, as they typically start with a lowercase letter and the following words start with uppercase, like actionPerformed() or print(). For variables, it harmonizes naming by combining words to express a single concept, like firstName, which makes it clear what data the variable represents .

You might also like