J# Lesson - Lesson 1: Introduction to J#
 
FunctionX Logo

Introduction to J#

 

Introduction

 

Overview of J#

Java is a computer language used to create applications on different operating systems, including Linux, MS Windows, Macintosh, etc. J#, pronounced J sharp, is Microsoft's implementation of Java with new concepts and a different flavor. It is very important to be aware that, although the codes of Java and J# on screen appear sometimes the same, both languages have a lot of differences and should not be considered the same. Neither Sun (the initiator and main, but not the only one, publisher of Java) nor Microsoft (the only publisher, at the time of this writing, of J#) made any effort or pretension to have both languages compatible (and in my opinion, it should never be an issue for you). The goal of J# is to make sure that people who program in Java can use it to write applications using the Microsoft .NET Framework, whether applications are GUI applications for the MS Windows, web-based applications, communication software, etc. J# allows Java developers to enjoy the same ease and friendly environment as Visual Basic .NET or Microsoft C# programmers. 

Microsoft provides J# in two ways: free or purchased.

Microsoft created a library called the Microsoft .NET Framework and made it freely available on some versions of MS Windows. For example, it is automatically installed with Microsoft Windows XP and Windows Server 2003. The library can also be installed or added to some other versions of MS Windows. The other way you can get J# is through Microsoft Visual Studio .NET.

A J# application displays its result on a black window referred to as the DOS prompt or DOS window. Such an application is called a console application. On this site, we will create only console applications.

Introduction to Objects

An object is an entity considered as one unit even if it happens to be made of different parts. For example, a book is represented as an object although it has such different aspects as the title, the number of pages, the type of cover, etc:

In computer languages, an object is created from a concept called class. In the J# language, everything is built as a class or around a class. Therefore, each program must have at least one class.

To create a class in a J# application, you type the class keyword followed by a name. Here is an example:

class Book

The actual building of a class starts with an opening curly bracket "{" and ends with a closing curly bracket "}". Therefore, a class can be created as follows:

class Book { }

Because a class can be made of numerous parts, listed as building blocks of the class, one line could become (too) crowded. Therefore, each curly bracket should be displayed on its own line:

class Book
{
}
  C++ Note

In C++, a variable  can be declared almost anywhere. For example, C++ has the concept of global variable. In J#, a variable can only be a member of a class.

 

A class as an object can also be made to handle assignments. For example, one of the jobs of a book is to present its pages to a reader. An assignment of a class is called a function. A function is simply a section of code that takes care of a particular detail for the functionality of the program. To create a function, you must specify its name, which starts with a letter or an underscore and can be followed by letters, digits, or underscores. The name of a function is followed by an opening and a closing parentheses. Here is an example:

class Book
{
PresentPages()
}

As mentioned already, a function's job is to carry a specific assignment within a program. As such, it could provide a value once the assignment has been carried. In Lesson 3, we will review the types of values that a function can provide. If on the other hand a function doesn't give back a value, then it is considered void. The type of value that a function can provide (or return) is written on the left side of the function name. An example would be:

class Book
{
void PresentPages()
}

The assignment that a function carries is included between an opening curly bracket "{" and a closing curly bracket "}". Therefore, a function can be written as follows:

class Book
{
void PresentPages()
{
}
}

Indentation is a technique of specifying where a line of code starts and where it ends. Although indentation is not strictly part of the J# language, it makes the program easier to read. Therefore, the above lines of code can be written as follows:

class Book
{
	void PresentPages()
	{
	}
}

Note that this time, it is easier to know where the class starts and ends, and where the function starts and ends. The most regularly used function of a J# program is called main. In fact, every program must have this main() function. Based on this, the minimum you can write a program with is as follows:

class SomeClassName
{
	void main()
	{
	}
}

In some cases (and we will see when and why), we will need to write a function as "static". As such, the main() function should always be created with the static word on its left as follows:

class SomeClassName
{
	static void main()
	{
	}
}

A computer program is started in a regular computer file. A file for a J# program has the extension .jsl. To make your programming fast, the J# language ships with many primary files that allow you to lay a foundation for your program.

 

The Compiler

You may have realized that all of the words we have used so far, such as class, void, or static, are regular English words that you can read and whose meaning you can find in a dictionary. Computer languages are created with meaningful words like that to make them easier to read and understand. Unfortunately, the computer doesn't understand those words and cannot execute the instructions those words represent. Like most others, J# is just a language that is used to give instructions to the computer. To make the computer understand these instructions, you need another, somehow more powerful, program. This program would receive the language's instructions in plain English and would "translate" them in another language that the computer can understand. This program is called a compiler.

A compiler is a computer program made of internal other programs that we can call components. One of the components, in fact probably the first component, of a compiler is called a parser. A parser "scans" a file that contains (part of) the program. It checks for syntax, keywords, unknown words, and some other routines. If the parser finds a problem, which could be anything, either it stops or it continues making a list of the mistakes it found. Then it displays this list to you to fix. Sometimes it would point to the exact line where the/a problem was found. Sometimes it would point to the line where the problem showed its impact although the problem may be found somewhere else. With experience, you will know how to fix the program or troubleshoot these problems. On this site, we will address as many issues as possible.

The compiler that ships with J# is a program called vjc. Like most other programs, it has the extension .exe. To locate the vjc application, in Windows Explorer or My Computer, you can open  the C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322 folder and you would see a vjc.exe icon. If you don't see it, you should download the J# software development kit from the Microsoft web site.

 

Practical LearningPractical Learning: Adding the Compiler

 
  1. Click Start -> Control Panel
  2. In Windows 2000 and XP (Home Edition or Professional), double-click System
    In Windows Server 2003, click System
  3. In the System Properties dialog box, click the Advanced tab and click Environment Variables
  4. In the System Variables section, click Path and click Edit
  5. Press Home and press the right arrow key. Check if you have an entry that appears as Microsoft.NET\Framework\vXXXX where XXXX represents some numbers.
    If you don't see that entry, press End or get to the end of the string

    Type ; followed by the complete path to the compiler. Mine appears as:
     
    %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\WINDOWS\Microsoft.NET\Framework\v2.0.40607
  6. Click OK on each dialog box
 

Creating a Program

 

On this site, we will use the free vjc.exe compiler that Microsoft provides. Using it, all of our programs will be created using a text editor, in this case Notepad. All of our programs will be compiled from the command prompt.

A J# application is primarily made of files called source files. A source file is a regular ASCII-based text file. In the case of J#, that file must have the extension .jsl

 

Practical LearningPractical Learning: Creating a Program

 
  1. Start Notepad
  2. In the empty file, type the following:
     
    public class Exercise
    {
    	public static void main(String[] args)
    	{
    
    	}
    }
  3. To save the file, on the main menu, click File -> Save
  4. Select and display the C: (or the D:) drive in the Save In combo box
  5. Click the Create New Folder button Create New Folder
  6. Type JSharp Lessons
  7. Press Enter twice or display the new folder in the Save In combo box
  8. Click the Create New Folder button Create New Folder again
  9. Type Exercise1 and press Enter twice or display the new folder in the Save In combo box
  10. Save the file as exercise.jsl
     
    Save As
  11. Click Save
 

Introduction to Packages

As mentioned already, an application consists of giving instructions to the computer. These instructions are written in a text file. An instruction starts with a character or a word and usually ends with a semi-colon.

When you create a J# application, you should create at least one J# file. If you create your application using the Console Application, a J# code file is automatically added to your project. Most of the time, an application is made of various files that must be considered as one entity. To include a group of files in one entity, you create what is called a package. On top of each file that belongs to a package, on top of each file, type the package keyword followed by a name and ending with a semi-colon. Here is an example:

package PackageName; 

To make the process of programming easier, a computer language such as J# ships with a series of files grouped in packages. These packages can be used to lay a foundation for your program. For example, it is very difficult to be able to display even the simplest character on the computer screen. To make this easily possible, J# ships with a package that contains classes that can take care of this for you. As mentioned already, a package is one or a series of files considered as an entity. These files contain J# code such as classes.

To use a class that is part of a package, you must know the name of the package and the name of the class. To access that class, type the name of the package, followed by the period operator, followed by the name of the class. This would be done as follows:

package Exercise;

public class ClassName
{
	public static void main()
	{
		PackageName.SomeClass
	}
}

We also mentioned that a class can have members. To access a member of a class that is contained in a package, type the name of the package, followed by a period operator, followed by the name of the class, followed by a period operator, and followed by the name of the member you want to access. Here is an example

package Exercise;

public class ClassName
{
	public static void main()
	{
		PackageName.SomeClass.AMemberOfThisClass;
	}
}

Practical LearningPractical Learning: Specifying a Package

 
  1. Change the program as follows:
     
    package Exercise1;
    
    public class Exercise
    {
    	public static void main(String[] args)
    	{
    
    	}
    }
    
  2. To test the application, open the Command Prompt and change to the folder in which you created the J# file:
  3. Type vjc exercise.jsl and press Enter
  4. When the program has finished compiling, type exercise and press Enter
     

     
    The program doesn't display anything since we didn't ask it to.
  5. Return to Notepad

The System Package

 

Introduction

To make programming in C# a little easier, many objects and functions have already been created and stored in various packages. Each package in J# is used to provide a specify set of classes. The most regularly used namespace in the J# language is called System.

Inside of the System package is a class called Console. The Console class is used to display things on the console screen also called the DOS window.

The Console class contains assignments (called functions) to display information on the screen or to retrieve information from the user who types it in the DOS window. The function that is used to display text on the screen is called Write. To use the Write() function, inside of the parentheses, type the sentence between double-quotes. Here is an example:

class Program
{
	public static void main()
	{
		System.Console.Write(0);
	}
}

Besides the Write() function, the Console class also provides a function called WriteLine(). The difference is that, after displaying something on the screen, the Write() function keeps the caret on the same line but the WriteLine() transfers the caret to the next line.

 

Practical Learning: Using the System Package

  1. To use the System package, in the file, change the body of the main() function as follows:
     
    package Exercise1;
    
    public class Exercise
    {
    	public static void main(String[] args)
    	{
    		System.Console.Write(12);
    	}
    }
  2. To execute the application, press Ctrl + F5
     
    12
  3. Notice that the program displays 12
  4. Return to Notepad

Importing a Package

To include one of the packages that ship with J# in one of the files of your program, on top of the file, type the import keyword followed by the name of that package, followed by .*;.

Practical Learning: Using the Import Keyword

  1. Return to Notepad and change the program as follows:
     
    package Exercise1;
    import System.*;
    
    public class Exercise
    {
    	public static void main(String[] args)
    	{
    		Console.Write(12);
    	}
    }
  2. Save the file and return to the Command Prompt
  3. Compile and test the program

Accessories for Coding

 

Comments

A comment is a line or paragraph of text that the compiler would not consider when examining the code of a program. There are two types of comments recognized by J#.

To display a comment on a line of text, start the line with two forward slashes //. Anything on the right side of // would be ignored. Here is an example:

// This line will be ignored. I can write in it anything I want

The above type of comment is used on only one line. You can also start a comment with /*. This type of comment ends with */. Anything between these two combinations would not be read by the compiler. Therefore, you can use this technique to span a comment on more than one line.

Escape Sequences

An escape sequence is a special character that displays non-visibly. For example, you can use this type of character to indicate the end of line, that is, to ask the program to continue on the next line. An escape sequence is represented by a backslash character, \, followed by another character or symbol. For example, the escape sequence that moves to the next line is \n.

An escape can be included in single-quotes as in '\n'. It can also be provided in double-quotes as "\n".

The J# language recognizes other escape sequences.

Escape Sequence Name ASCII value Description
\a Bell (alert) 007 Makes a sound from the computer
\b Backspace 008 Takes the cursor back
\t Horizontal Tab 009 Takes the cursor to the next tab stop
\n New line 010 Takes the cursor to the beginning of the next line
\v Vertical Tab 011 Performs a vertical tab
\f Form feed 012  
\r Carriage return 013 Causes a carriage return
\" Double Quote 034 Displays a quotation mark (")
\' Apostrophe 039 Displays an apostrophe (')
\? Question mark 063 Displays a question mark
\\ Backslash 092 Displays a backslash (\)
\0 Null 000 Displays a null character
 

Practical Learning: Using Escape Sequences

  1. To use the Line Feed escape sequence, in Notepad, change the line of code in the Main() function as follows:
     
    package Exercise1;
    import System.*;
    
    public class Exercise
    {
    	public static void main(String[] args)
    	{
    		Console.Write("\t");
    		Console.Write(12);
    		Console.Write("\n");
    	}
    }
  2. Save the file and return to the Command Prompt
  3. Compile and test the program
  4. To finish, type Exit and press Enter
 

Copyright © 2005 FunctionX, Inc. Next