HelloWorld in Java

Background

Let us write a little starter app in Java.

 

HelloWorld

Code


public class helloWorld 
{

    public static void main(String[] args) 
    {
        // Prints "Hello, World" to the terminal window.
        System.out.println("Hello, World");
    }

}


Compile


javac helloWorld.java

Run


java helloWorld

Output

Output – Image

Output – Text


>java helloWorld
Hello, World

 

HelloWorldContext

Code


import java.net.InetAddress;

public class helloWorldContext 
{

    public static void main(String[] args) 
    {
        
        StringBuilder sbHostname = new StringBuilder();

        String strUsername = null;
        String strHostname = null;
        int    iRC = 0;
        
        
        String FORMAT_HELLO_USERNAME = "Hello %s!  I am %s%n";
        String FORMAT_NETWORK_HOSTNAME = "%s is %s %n";
            
        strUsername = System.getProperty("user.name");
        
        System.out.printf
            (
                  FORMAT_HELLO_USERNAME
                , "World"
                , strUsername
            );
        
        
        iRC = getNetworkInfo
        (
            sbHostname
        );
        
 
        if (
                   ( iRC == 0)
                && (sbHostname != null)
            )
        {
            
            //get string
            strHostname = sbHostname.toString();
            
            System.out.printf
                (
                      FORMAT_NETWORK_HOSTNAME
                    , "Hostname"
                    , strHostname
                );
        }           
            
    }
    
    private static int getNetworkInfo
                        (
                            StringBuilder sbHostname
                        )
    {
        int iRC =0;
        InetAddress inetAddress;
        String      strHostname;
        
        try
        {
             /* 
                Get Network Object
             */           
             inetAddress = InetAddress.getLocalHost();
             
             if (inetAddress != null)
             {
                 
                /* 
                    Get Network Hostname
                */           
                strHostname = inetAddress.getHostName();
                 
                /* 
                    Capture Network Hostname into StringBuffer
                */  
                if (strHostname != null)
                {
                    sbHostname.append(strHostname);
                }
                
                iRC = 0;
                
             }
        }
        catch (java.net.UnknownHostException ex) 
        {
            iRC =-1;
            
            ex.printStackTrace();
        }
        
        return (iRC);
        
    }

}



Compile


javac helloWorldContext.java

Run


java helloWorldContext

Output

Output – Image

 

Output – Text


>java helloWorldContext
Hello World!  I am dadeniji
Hostname is lab

Source Code Control

GitHub

  1. Gist
    • helloWorldInJava
      Link
  2.  Repository
    • DanielAdeniji / helloWorldInJava
      Link

3 thoughts on “HelloWorld in Java

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s